This example shows how we can use the Apache Commons IO library to simplify a file copy process.
package org.kodejava.example.commons.io;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
// The source file name to be copied.
File source = new File("january.doc");
// The target file name to which the source file will be copied.
File target = new File("january-backup.doc");
// A temporary folder where we are gonna copy the source file to.
// Here we use the temporary folder of the OS, which can be
// obtained using java.io.tmpdir property.
File targetDir = new File(System.getProperty("java.io.tmpdir"));
try {
// Using FileUtils.copyFile() method to copy a file.
System.out.println("Copying " + source + " file to " + target);
FileUtils.copyFile(source, target);
// To copy a file to a specified folder we can use the
// FileUtils.copyFileToDirectory() method.
System.out.println("Copying " + source + " file to " + targetDir);
FileUtils.copyFileToDirectory(source, targetDir);
} catch (IOException e) {
// Errors will be reported here if any error occurs during
// copying the file
e.printStackTrace();
}
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I clear the current command line in terminal? - February 14, 2019
- How do I generate random alphanumeric strings? - February 7, 2019
- Why do I get ArrayIndexOutOfBoundsException in Java? - January 29, 2019