Copy File in Java

In this tutorial, we will learn different ways to copy one file into another file in Java.

Create two files named “sample1.txt” and “sample2.txt” in your file system. In this example, we will copy the “sample1.txt” file into the “sample2.txt” file.

Java Copy File with FileInputStream & FileOutputStream

With FileInputStream and FileOutputStream we create streams for reading and writing to a File. When the file is not found, FileNotFoundException is thrown. File is a representation of a file or directory in Java.

The example copies a file using FileInputStream, FileOutputStream, and File.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFileStream {

    public static void main(String[] args) throws IOException {

        File source = new File("C:/workspace/sample1.txt");
        File dest = new File("C:/workspace/sample2.txt");

        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(dest)) {

            byte[] buffer = new byte[1024];
            int length;

            while ((length = fis.read(buffer)) > 0) {

                fos.write(buffer, 0, length);
            }
        }
    }
}

Java Copy File with Files.copy

Java 7 introduced the Files.copy method, which provides an easy way of copying a file. The copy fails if the target file exists unless the REPLACE_EXISTING option is specified. Files.copy takes an optional third copy options argument.

The example copies a file with Files.copy. It replaces the destination if it already exists.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class CopyFileStream {

    public static void main(String[] args) throws IOException {

        File source = new File("C:/workspace/sample1.txt");
        File dest = new File("C:/workspace/sample2.txt");

        Files.copy(source.toPath(), dest.toPath(),
                StandardCopyOption.REPLACE_EXISTING);
    }
}

Java Copy File with Apache Commons IO

Apache Commons IO is a library of utilities to assist with developing IO functionality. It contains the FileUtils.copyFile method to perform copying. FileUtils.copyFile copies the contents of the specified source file to the specified destination file preserving the file date. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class CopyFileStream {

    public static void main(String[] args) throws IOException {

        File source = new File("C:/workspace/sample1.txt");
        File dest = new File("C:/workspace/sample2.txt");

        FileUtils.copyFile(source, dest);
    }
}

Java Copy File with Guava

Google Guava is an open-source set of common libraries for Java. It includes Files.copy for copying a file.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>
The example copies a file with Guava's Files.copy method:
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;

public class CopyFileStream {

    public static void main(String[] args) throws IOException {

        File source = new File("C:/workspace/sample1.txt");
        File dest = new File("C:/workspace/sample2.txt");

        Files.copy(source, dest);
    }
}

File Handling Examples

Comments