java 如何将文件移动到非空目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26678539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:23:17  来源:igfitidea点击:

How can I move a file to a non-empty directory?

javanio

提问by user2406854

I'm new to Java's nio package and I can't figure out how to get a file from one directory into another. My program is supposed to read through a directory and its subdirectories and process files based on certain conditions. I can get all the files using Files.walkFileTree but when I try to move them I get a java.nio.file.AccessDeniedException.

我是 Java 的 nio 包的新手,我不知道如何将文件从一个目录获取到另一个目录。我的程序应该根据某些条件读取目录及其子目录并处理文件。我可以使用 Files.walkFileTree 获取所有文件,但是当我尝试移动它们时,我得到一个 java.nio.file.AccessDeniedException。

If I try to copy them, I get a DirectoryNotEmptyException. I haven't been able to find any help on Google. I'm sure there must an easy way to move a file from one directory to another, but I can't figure it out.

如果我尝试复制它们,则会收到 DirectoryNotEmptyException。我无法在 Google 上找到任何帮助。我确信必须有一种简单的方法将文件从一个目录移动到另一个目录,但我无法弄清楚。

This is what I'm trying that gets the DirectoryNotEmptyException:

这就是我正在尝试获取 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();
            }
        }
    }
}

I'm iterating through a list of files, trying to match a .meta file with a .pdf of the same name. Once I find the match, I move the metadata file to the directory that has the pdf.

我正在遍历文件列表,尝试将 .meta 文件与同名的 .pdf 匹配。找到匹配项后,我将元数据文件移动到包含 pdf 的目录。

I get this exception: java.nio.file.DirectoryNotEmptyException: C:\test\CGBA-RAC\Part-A at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372) at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287) at java.nio.file.Files.move(Files.java:1347) at cgba.rac.errorprocessor.ErrorProcessor.findMatchingPdf(ErrorProcessor.java:149) at cgba.rac.errorprocessor.ErrorProcessor.matchErrorFile(ErrorProcessor.java:81) at cgba.rac.errorprocessor.ErrorProcessor.main(ErrorProcessor.java:36)

我得到这个异常: java.nio.file.DirectoryNotEmptyException: C:\test\CGBA-RAC\Part-A at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372) at sun.nio.fs.WindowsFileSystemProvider .move(WindowsFileSystemProvider.java:287) at java.nio.file.Files.move(Files.java:1347) at cgba.rac.errorprocessor.ErrorProcessor.findMatchingPdf(ErrorProcessor.java:149) at cgba.rac.errorprocessor。 ErrorProcessor.matchErrorFile(ErrorProcessor.java:81) 在 cgba.rac.errorprocessor.ErrorProcessor.main(ErrorProcessor.java:36)

回答by Kenster

Files.move(file, cgbaFile.getParent(), StandardCopyOption.REPLACE_EXISTING);

For the target, you're providing the directory you want to move the file into. This is incorrect. The target should be the new pathname that you want the file to have--the new directory plus the filename.

对于目标,您提供了要将文件移动到的目录。这是不正确的。目标应该是您希望文件具有的新路径名——新目录加上文件名。

For example, suppose you wanted to move /tmp/foo.txtto the /var/tmpdirectory. You're calling Files.move("/tmp/foo.txt", "/var/tmp")when you should be calling Files.move("/tmp/foo.txt", "/var/tmp/foo.txt").

例如,假设您想移动/tmp/foo.txt到该/var/tmp目录。你在Files.move("/tmp/foo.txt", "/var/tmp")应该打电话的时候打电话Files.move("/tmp/foo.txt", "/var/tmp/foo.txt")

You're getting that specific error because the JVM is trying to delete the target directory in order to replace it with the file.

您收到该特定错误是因为 JVM 试图删除目标目录以将其替换为文件。

One of these ought to generate the correct target path:

其中之一应该生成正确的目标路径:

Path target = cgbaFile.resolveSibling(file.getFileName());

Path target = cgbaFile.getParent().resolve(file.getFileName());

回答by Ferrakkem Bhuiyan

Path source = Paths.get("Var");
Path target = Paths.get("Fot", "Var");
try {
    Files.move(
        source,
        target,  
        StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
    e.printStackTrace();
}

java.nio.file.Files is a necessity, so here is the edited solution. Please see if it works coz I have never used the new Files class before

java.nio.file.Files 是必要的,所以这里是编辑的解决方案。请看看它是否有效因为我以前从未使用过新的 Files 类