有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java移动文件时出错,进程无法访问该文件,因为另一个进程正在使用该文件

File file = new File(FILE_DIR);
    File[] files = file.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
        if (name.toLowerCase().endsWith(".pdf")) {
            return true;
        } else {
            return false;
        }
     }
    });

下面我在目录中循环,这样我就可以将一个文件移动到另一个目录

for (File name : files) {
    int i = 1;
    System.out.println("PDF present in folder  " + i + ".  " + name.getName());
    Path temp = Files.move(Paths.get(FILE_DIR + name.getName()),
                    Paths.get(LocalFilePathSuccess + name.getName()));

   if (temp != null) {
       System.out.println("File renamed and moved successfully");
   } else {
       System.out.println("Failed to move the file");
   }

但是当我运行代码时,我得到如下错误: **

java.nio.file.FileSystemException: ---> The process cannot access the file because it is being used by another process. **

从这个错误我知道,特定的文件正在处理中,这就是为什么我们不能移动它

我想要的是, 如何停止此文件进程,然后移动文件

提前感谢


共 (0) 个答案