Does Files.copy(Path,Path) create directories?
Asked Answered
T

2

18

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

Path path = Paths.get(textPath);

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

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

result in ss1.text being copied to a new directory?

Titanesque answered 13/4, 2015 at 3:34 Comment(2)
I just keep getting IO exceptionsTitanesque
Why not read their message?Insulin
L
10

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.

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.

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.

Or you can simply use `FileUtils class from apache http://commons.apache.org/proper/commons-io/ library, available since version 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();
}
Latialatices answered 13/4, 2015 at 5:52 Comment(1)
If I am not wrong, commons-io library does not use the java.nio (at least, not for all the actions) package, but the java.io. I am saying this because you mentioned to use the java.nio package, but afterwards you mention the commons-io library. I would also recommend the java.nio because it has better file handling than java.io - I had some problems mostly with reading files that have special characters in their names which nio handled without problems.Leix
E
27

This is very easy with 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...

Eskil answered 30/4, 2019 at 11:18 Comment(0)
L
10

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.

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.

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.

Or you can simply use `FileUtils class from apache http://commons.apache.org/proper/commons-io/ library, available since version 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();
}
Latialatices answered 13/4, 2015 at 5:52 Comment(1)
If I am not wrong, commons-io library does not use the java.nio (at least, not for all the actions) package, but the java.io. I am saying this because you mentioned to use the java.nio package, but afterwards you mention the commons-io library. I would also recommend the java.nio because it has better file handling than java.io - I had some problems mostly with reading files that have special characters in their names which nio handled without problems.Leix

© 2022 - 2024 — McMap. All rights reserved.