当前位置: 首页 > 知识库问答 >
问题:

重命名目录中的文件时出错:“该进程无法访问该文件,因为它正被另一个进程使用。”

钮长恨
2023-03-14
import java.io.File;
import java.io.IOException;
import java.util.*;
//import java.lang.Object;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class AppendTool {
public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    System.out.println("Input Directory to Append Values: ");
    String dirInput = sc.nextLine();
    System.out.println("Input value to append to Directory Files: ");
    String valInput = sc.nextLine();
    
    sc.close();
    
    Path source = Paths.get(dirInput);
    
    File path = new File(dirInput);

    File [] files = path.listFiles();
    for (int i = 0; i < files.length; i++){
        if (files[i].isFile()){ //this line weeds out other directories/folders
            System.out.println(files[i]);
       try {     
           // int where = files[i].getName().lastIndexOf(".");
            String result = valInput + files[i].getName();//.substring(0, where) + files[i].getName().substring(where);
            System.out.println(result);
            File dest = new File(result);
           //Files.move(files[i].toPath(), dest.toPath().resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);   
            Files.move(source, source.resolveSibling(dest.getName()), StandardCopyOption.REPLACE_EXISTING);
            System.out.println(dest);
            return;
       } catch (IOException e) {
            e.printStackTrace();
          }
            }   
    }
  }
 }

我试图遍历目录中的所有文件,并为每个文件名添加一个值。使用文件。move会创建一个FileSystemException,并声明该文件正被另一个进程使用。使用注释掉的文件。移动(文件[i].toPath…)删除创建新文件,从目录中删除旧文件,但不会替换原始文件。任何帮助都将不胜感激。对于下面的错误,我有一个东西。txt文件保存在文档中,我想在文件名后面加上“e”。

C:\用户\Bob\文档-

在爪哇。基地/太阳。尼奥。财政司司长。Windows例外。java上的translateToIOException(WindowsException.java:92)。基地/太阳。尼奥。财政司司长。Windows例外。java上的rethrowAsIOException(WindowsException.java:103)。基地/太阳。尼奥。财政司司长。WindowsFileCopy。在java上移动(WindowsFileCopy.java:395)。基地/太阳。尼奥。财政司司长。WindowsFileSystemProvider。在java上移动(WindowsFileSystemProvider.java:292)。base/java。尼奥。文件文件夹。在AppendTool上移动(Files.java:1426)。main(AppendTool.java:36)

共有1个答案

仲孙信瑞
2023-03-14

由于文件的参数参数不正确,因此出现错误。move()方法是错误的。您需要提供源文件(原始文件名)和文件的完整路径和文件名。resolveSibling()方法需要提供目标文件的完整路径和文件名,在本例中,目标文件发生在原始文件名上,并在其开头插入所需的文本。

下面是一个如何实现这一点的快速示例:

Scanner sc = new Scanner(System.in);
System.out.println("Input Directory path to modify file names in:");
System.out.print("Directory Path: --> ");
String dirInput = sc.nextLine();

/* 'DO' take the time to ensure that OS file naming rules 
   are strictly followed when accepting User Input!!   */
System.out.print("Enter the text you want to insert into\n"
               + "the begining of each file name within\n"
               + "this directory: --> ");
String valInput = sc.nextLine();

// Close ONLY if you won't ever need it again in this application.
sc.close(); 
System.out.println();
    
File path = new File(dirInput);
File[] files = path.listFiles();

for (File file : files) {
    Path source = Paths.get(file.getAbsolutePath());
    File newName = new File(file.getParent() + File.separator + valInput + file.getName());
    System.out.println(newName.getAbsolutePath());
    try {
        Files.move(source, source.resolveSibling(newName.getAbsolutePath()),
                                       StandardCopyOption.REPLACE_EXISTING);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
 类似资料:
  • 下面是使用Ucanaccess Jdbc驱动程序从Microsoft Access文件filename.accdb获取连接的代码。但在运行此代码时,它会抛出异常,就像已经使用的文件一样。 但是我想在其他应用程序使用MSAccess数据库文件时并发地使用它。 当我运行上面的代码时,出现了如下异常: net.ucanaccess.jdbc.ucanaccesssqlexception:UCAEXC::

  • 我的脚本搜索特定目录中的所有pdf文件,然后从pdf中提取一个id,并在文件中组织pdf。例如我有: 我想这样组织它们: 下面的脚本做的工作,但我认为只有最后一个文件输出以下错误: 回溯(最近一次调用):文件“C:\Users\user\Downloads\aa\project.py”,第74行,在操作系统中。rename(source,dest)PermissionError:[WinError

  • 问题内容: 我的代码用于一个脚本,该脚本查看一个文件夹并删除分辨率为1920x1080的图像。我的问题是我的代码运行时; 我收到此错误消息: 只需确认一下,Python是我计算机上运行的唯一程序。是什么导致此问题,我该如何解决? 问题答案: 您的过程就是打开文件的过程(仍然存在)。您需要先关闭它,然后再删除它。 我不知道PIL是否支持上下文,但是是否支持: 进入之前,请确保删除(并关闭文件)。 如

  • 我正在测试一个python代码,它将文件从源路径移动到目标路径。测试是使用Python3中的pytest完成的。但我在这里面临着一个障碍。就是这样,我试图在代码结束时删除源路径和目标路径。为此,我使用了类似shutil的命令。rmtree(路径)或操作系统。rmdir(路径)。这导致了错误-“[WinError 32]该进程无法访问该文件,因为它正被另一进程使用”。请帮我做这个。下面是python

  • 我正在尝试测试我自己的antiweb版本,可以在这里找到。但是,我正在使用Pythons单元测试模块对其进行测试。代码如下: 除了功能外,所有功能都正常工作。在执行unittest时,如果不拆下,temp文件夹及其内容将被完美创建。但是使用功能,我得到一个错误: 当我再看临时文件夹时,文件夹本身仍然在那里,但现在是空的。这将是太多,包括我的反网络文件在这里,所以我有它在这里再次链接,如果你需要它。

  • 我的代码用于查看文件夹并删除分辨率为1920x1080的图像的脚本。我遇到的问题是,当我的代码运行时; 我收到以下错误消息: 我想确认一下,Python是我电脑上唯一运行的程序。导致此问题的原因是什么?如何解决?