Java中的Files.move()给出了FilesSystemException错误,因为文件夹“正被另一个进程使用".但这不是 [英] Files.move() in Java giving a FilesSystemException error because folder is "being used by another process," but it's not

查看:911
本文介绍了Java中的Files.move()给出了FilesSystemException错误,因为文件夹“正被另一个进程使用".但这不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个文件移动到另一个文件夹,但是我不能,因为该文件正被另一个进程使用." 这是我的代码:

I want to move a single file to another folder, but I can't because "it is being used by another process." This is my code:

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:该进程无法访问该文件,因为该文件正在被另一个进程使用.

Error message:
Exception in thread "main" java.nio.file.FileSystemException: C:\folderpath: The process cannot access the file because it is being used by another process.

我尝试了不同的文件,不同的文件夹,但是每次都说该文件正在使用中.我已经用一个基本的文本文件测试了它,该文件绝对是关闭的,并且在测试时没有被使用,但是仍然出现错误.有人知道发生了什么吗?或者,还有另一种方法来移动没有此问题的文件吗?

I've tried out different files, different folders, but everytime it says the file is being used. I've tested it with a basic text file that was definitely closed and not being used when I tested it, but I still get the error. Does anyone know what's going on? Or, is there another way to move files that won't have this issue?

推荐答案

用户 rollback 的答复:

Files.move(myFile.toPath(), myFolder.toPath().resolve(myFile.getName()), StandardCopyOption.REPLACE_EXISTING);

这篇关于Java中的Files.move()给出了FilesSystemException错误,因为文件夹“正被另一个进程使用".但这不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆