我是Java的nio包的新手,我无法弄清楚如何从一个目录到另一个目录中获取文件.我的程序应该根据某些条件读取目录及其子目录和进程文件.我可以使用Files.walkFileTree获取所有文件但是当我尝试移动它们时,我得到一个java.nio.file.AccessDeniedException.
如果我尝试复制它们,我会收到DirectoryNotEmptyException.我无法在Google上找到任何帮助.我确信必须有一种简单的方法将文件从一个目录移动到另一个目录,但我无法弄清楚.
这是我正在尝试获取DirectoryNotEmptyException:
private static void findMatchingPdf(Path file, ArrayList cgbaFiles) {
Iterator iter = cgbaFiles.iterator();
String pdfOfFile = file.getFileName().toString().substring(0, file.getFileName().toString().length() - 5) + ".pdf";
while (iter.hasNext()){
Path cgbaFile = (Path) iter.next();
if (cgbaFile.getFileName().toString().equals(pdfOfFile)) {
try {
Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在遍历文件列表,尝试将.meta文件与同名的.pdf匹配.找到匹配后,我将元数据文件移动到包含pdf的目录.
我得到此异常:sun.nio.fs.WindowsFileSystemProvider中的sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372)中的java.nio.file.DirectoryNotEmptyException:C:\ test\CGBA-RAC\Part-A .move(WindowsFileSystemProvider.java:287)位于cgba.rac.errorprocessor的cgba.rac.errorprocessor.ErrorProcessor.findMatchingPdf(ErrorProcessor.java:149)的java.nio.file.Files.move(Files.java:1347).错误处理器.matchErrorFile(ErrorProcessor.java:81)at cgba.rac.errorprocessor.ErrorProcessor.main(ErrorProcessor.java:36)
Ken*_*ter 22
Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING);
Run Code Online (Sandbox Code Playgroud)
对于目标,您将提供要将文件移动到的目录.这是不正确的.目标应该是您希望文件具有的新路径名 - 新目录加上文件名.
例如,假设您要移动/tmp/foo.txt到/var/tmp目录.Files.move("/tmp/foo.txt", "/var/tmp")你应该打电话的时候打电话Files.move("/tmp/foo.txt", "/var/tmp/foo.txt").
您正在获取该特定错误,因为JVM正在尝试删除目标目录以便将其替换为该文件.
其中一个应该生成正确的目标路径:
Path target = cgbaFile.resolveSibling(file.getFileName());
Path target = cgbaFile.getParent().resolve(file.getFileName());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16038 次 |
| 最近记录: |