java Files.copy(Path,Path) 是否创建目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29597454/
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 15:34:20  来源:igfitidea点击:

Does Files.copy(Path,Path) create directories?

javatext-filesfile-copying

提问by knowads

I have a bunch of text files(say ss1.txt,ss2.txt,ss3.txt etc.) under a directory with my Java program (C:/Users/java/dir1)?
I want to move my txt files to a new directory that hasn't been created yet. I have a String address for all of my files and I think I can turn them into Paths using

我的 Java 程序 ( C:/Users/java/dir1)目录下有一堆文本文件(比如 ss1.txt、ss2.txt、ss3.txt 等)?
我想将我的 txt 文件移动到尚未创建的新目录中。我的所有文件都有一个字符串地址,我想我可以使用

Path path = Paths.get(textPath);

路径路径 = Paths.get(textPath);

Would creating a String (C:/Users/java/dir2), turning that into a path using the above method and then using

将创建一个字符串(C:/Users/java/dir2),使用上述方法将其转换为路径,然后使用

Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)

Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)

result in ss1.textbeing copied to a new directory?

结果ss1.text被复制到一个新目录?

采纳答案by Mateusz Sroka

Method Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)will not create directory, it will create file dir2 in directory java that will contain ss1.txt data.

方法Files.copy(C:/Users/java/dir1/ss1.txt,C:/Users/java/dir2)不会创建目录,它会在包含 ss1.txt 数据的目录 java 中创建文件 dir2。

You could try it with this code:

你可以用这个代码试试:

File sourceFile = new File( "C:/Users/java/dir1/ss1.txt" );
Path sourcePath = sourceFile.toPath();

File destFile = new File( "C:/Users/java/dir2" );
Path destPath = destFile.toPath();

Files.copy( sourcePath, destPath );

Remember use java.nio.file.Files and java.nio.file.Path.

记住使用 java.nio.file.Files 和 java.nio.file.Path。

If you want to use class form java.nio to copy files from one directory to other you should use Files.walkFileTree(...) method. You can see solution here Java: Using nio Files.copy to Move Directory.

如果您想使用类形式 java.nio 将文件从一个目录复制到另一个目录,您应该使用 Files.walkFileTree(...) 方法。您可以在此处查看解决方案Java: Using nio Files.copy to Move Directory

Or you can simply use `FileUtils class from apache http://commons.apache.org/proper/commons-io/library, available since version 1.2.

或者,您可以简单地使用 apache http://commons.apache.org/proper/commons-io/库中的`FileUtils 类,该类从1.2 版开始可用。

File source = new File("C:/Users/java/dir1");
File dest = new File("C:/Users/java/dir2");
try {
    FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
    e.printStackTrace();
}

回答by Daniel De León

This is very easy with Files.createDirectories()

这很容易使用Files.createDirectories()

Path source = Path.of("c:/dir/dir-x/file.ext");
Path target = Path.of("c:/target-dir/dir-y/target-file.ext");
Files.createDirectories(target.getParent());
Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING);    

And do not worry if the directories already exist, in that case it will do nothing and keep going...

并且不要担心目录是否已经存在,在这种情况下它什么都不做并继续......