Copying a File in Java

Copying a file from one place to another in Java is a common task that we need to do in the applications. In this Java tutorial, we will see different ways to copy a file in Java.

Note that file copying is not an atomic operation – in the case of an I/O error, power loss, process termination, or other problems, the copying operation is not complete. If we need to guard against those conditions, we should employ other file-level synchronization.

In all given examples, we will be copying the content of testoriginal.txt to an another file testcopied.txt. The name and the location of the files can be replaced in any of the examples.

1. Using NIO Files.copy()

The Files class is in java.nio.file package. It provides the static methods that operate on files, directories, or other types of files.

  • By default, the copy fails if the target file already exists or is a symbolic link, except if the source and target are the same files, in which case the method completes without copying the file.
  • If the file is a directory then it creates an empty directory in the target location (entries in the directory are not copied).

Use one or more CopyOption enums to control how the copy should be done.

  • REPLACE_EXISTING : Any existing target file will be replaced.
  • COPY_ATTRIBUTES : Copy the file attributes from the source to the target file.
  • NOFOLLOW_LINKS : If the file is a symbolic link, then the symbolic link itself, not the target of the link, is copied.
Path source = Paths.get("c:/temp/testoriginal.txt");
Path target = Paths.get("c:/temp/testcopied.txt");

Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

2. Using FileChannel.transferTo()

If you are fond of FileChannel class for their brilliant performance, use this method. The key advantage here is that the JVM uses the OS’s access to DMA (Direct Memory Access) if present.

Using this technique, the data goes straight to/from disc to the bus, and then to the destination… bypassing any circuit through RAM or the CPU.

File fileToCopy = new File("c:/temp/testoriginal.txt");
FileInputStream inputStream = new FileInputStream(fileToCopy);
FileChannel inChannel = inputStream.getChannel();

File newFile = new File("c:/temp/testcopied.txt");
FileOutputStream outputStream = new FileOutputStream(newFile);
FileChannel outChannel = outputStream.getChannel();

inChannel.transferTo(0, fileToCopy.length(), outChannel);

inputStream.close();
outputStream.close();

3. Using Commons IO’s FileUtils

To use Apache Commons IO, we will need to download the commons-io dependency and include in the project.

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

Use one of the following classes for copying one file to another.

  • FileUtils – Internally it uses the java.nio.file.Files class so it is equivalent to use the java.nio.file.Files.copy() function.
  • IOUtils – It copies bytes from a large (over 2GB) InputStream to an OutputStream. This method uses the provided buffer, so there is no need to use a BufferedInputStream.
File fileToCopy = new File("c:/temp/testoriginal.txt");
File newFile = new File("c:/temp/testcopied.txt");

FileUtils.copyFile(fileToCopy, newFile);

// OR

IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile));

4. Using Guava’s Files

To use Guava, we will need to download the com.google.guava dependency and include in the project.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

The Files class provides utility methods for working with files. The Files.copy() method copies all the bytes from one file to another.

File fileToCopy = new File("c:/temp/testoriginal.txt");
File newFile = new File("c:/temp/testcopied.txt");

Files.copy(fileToCopy, newFile);

After Java 7, there have not been any major improvements in the Java IO package. So for any later Java release (Java 8 to Java 14), we have to reply on above-listed techniques.

Happy Learning !!

Source Code on Github

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.