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();
		}
	}
}
Category >> I/O  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>