我是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操作的结果.有人可以指出我的操作出错了吗?谢谢!