I want to copy files from one directory to another (subdirectory) using Java. I have a directory, dir, with text files. I iterate over the first 20 files in dir, and want to copy them to another directory in the dir directory, which I have created right before the iteration.
In the code, I want to copy the review
(which represents the ith text file or review) to trainingDir
. How can I do this? There seems not to be such a function (or I couldn't find). Thank you.
boolean success = false;
File[] reviews = dir.listFiles();
String trainingDir = dir.getAbsolutePath() + "/trainingData";
File trDir = new File(trainingDir);
success = trDir.mkdir();
for(int i = 1; i <= 20; i++) {
File review = reviews[i];
}
Source: https://docs.oracle.com/javase/tutorial/essential/io/copy.html
following code to copy files from one directory to another
You can use the following code to copy files from one directory to another
apache commons Fileutils is handy. you can do below activities.
copying file from one directory to another directory.
use
copyFileToDirectory(File srcFile, File destDir)
copying directory from one directory to another directory.
use
copyDirectory(File srcDir, File destDir)
copying contents of one file to another
use
static void copyFile(File srcFile, File destFile)