相关疑难解决方法(0)

将整个目录内容复制到另一个目录?

将整个目录内容复制到java或groovy中的另一个目录的方法?

java file-io grails groovy

63
推荐指数
6
解决办法
13万
查看次数

Java:使用nio Files.copy移动目录

我是nio类的新手,无法将文件目录移动到新创建的目录中.

我首先创建2个目录:

File sourceDir = new File(sourceDirStr); //this directory already exists
File destDir = new File(destDirectoryStr); //this is a new directory
Run Code Online (Sandbox Code Playgroud)

然后我尝试将现有文件复制到新目录中,使用:

Path destPath = destDir.toPath();
for (int i = 0; i < sourceSize; i++) {
    Path sourcePath = sourceDir.listFiles()[i].toPath();
    Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误:

Exception in thread "main" java.nio.file.FileSystemException: destDir/Experiment.log: Not a directory
Run Code Online (Sandbox Code Playgroud)

我知道这destDir/Experiment.log不是现有的目录; 它应该是一个新的文件作为Files.copy操作的结果.有人可以指出我的操作出错了吗?谢谢!

java nio file

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×2

file ×1

file-io ×1

grails ×1

groovy ×1

nio ×1