2

我想将单个文件移动到另一个文件夹,但我不能,因为“它正在被另一个进程使用”。这是我的代码:

static File myFile = new File("C:\\filepath");
static File myFolder = new File("C:\\folderpath");

public static void main(String[] args) 
        throws IOException {
    fileMove();
}

public static void fileMove() 
        throws IOException {
    Files.move(myFile.toPath(), myFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);
    return;
}

错误消息:
线程“main”中的异常 java.nio.file.FileSystemException:C:\folderpath:该进程无法访问该文件,因为它正被另一个进程使用。

我尝试了不同的文件,不同的文件夹,但每次都说该文件正在被使用。我已经用一个基本的文本文件对其进行了测试,该文件在我测试它时肯定已关闭并且没有被使用,但我仍然得到错误。有谁知道发生了什么?或者,是否有另一种方法可以移动不会出现此问题的文件?

4

2 回答 2

0

用户回滚的回答:

Files.move(myFile.toPath(), myFolder.toPath().resolve(myFile.getName()), StandardCopyOption.REPLACE_EXISTING);
于 2017-10-05T23:13:47.773 回答
-1

我用:

public static void moveFile(String origen, String destino) throws IOException {
        Path FROM = Paths.get(origen);
        Path TO = Paths.get(destino);

        CopyOption[] options = new CopyOption[]{
            StandardCopyOption.ATOMIC_MOVE

        };
        Files.move(FROM, TO, options);
    }
于 2017-10-05T23:15:13.360 回答