Java 7 File Copy Example Code

Before Java 7, if we need to copy a file we need to make a method or call the copyFile(File srcFile, File destFile) method of FileUtils inapache commons io package.

In Java 7, file copy function is very simple. The following is a code example for showing how to use Files.copy() method:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class FileCopyTest {
	public static void main(String[] args) {
		Path source = Paths.get("/Users/apple/Desktop/test.rtf");
		Path destination = Paths.get("/Users/apple/Desktop/copied.rtf");
 
		try {
			Files.copy(source, destination);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Leave a Comment