It seems barely credible that it's taken until now for the authors of Java to make such simple and everyday operations as copying files  possible in just a small number of lines of code without requiring developers to use 3rd party libraries. But here's how to do it in Java 7. Note that some hoops must be jumped through in order to turn the parameters into the necessary Path types. The code is HERE


import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Copier {
    public static void main(String[] args) throws IOException {
        //Copy file /foo/x.txt to /bar directory
        String source = "./foo/x.txt";
        String target = "./bar/x.txt";
        Copier.copyFile(source, target);

    public static void copyFile(String source, String target) {
        try {
            Files.copy(new File(source).toPath(), new File(target).toPath(),
        } catch (IOException e) {

Please support this site using the button below: