How to copy files to different directories with different file names in Java

Asked 2 years ago, Updated 2 years ago, 91 views

I'd like to copy the file from Java to another directory.

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];

}

There are about 20 files like this, and I want to move them all to a different directory. How shall I do it?

copy directory file java

2022-09-22 14:39

1 Answers

Use the Apache common library FileUtils

File source = new File("H:\\work-temp\\file");
File dest = new File("H:\\work-temp\\file2");
try {
    FileUtils.copyDirectory(source, dest);
} } catch (IOException e) {
    e.printStackTrace();
}

Try it this way.


2022-09-22 14:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.