Reputation: 21
In my Java program I have 2 paths (Strings) of 2 different directories.
I want a method to copy all the files from one directory to the other. (just the contents, not the folder).
How can I do that?
Upvotes: 0
Views: 67
Reputation: 29237
Can use
org.apache.commons.io.FileUtils;
Collection getAllFilesThatMatchFilenameExtension(String directoryName, String extension)
{
File directory = new File(directoryName);
return FileUtils.listFiles(directory, new WildcardFileFilter(extension), null);
}
loop through the collection and call
FileUtils.copyFileToDirectory(file, destinationDir);
Upvotes: 0
Reputation: 1123
Make a class that extends SimpleFileVisitor<Path>
and override its methods. visitFile
should copy the file to the new directory and the VisitDirectory
methods should just continue down the tree. Then use the new class with Files.walkFileTree
.
Upvotes: 1