Java Copy File

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

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.

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.

Copying File with 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.

Use the StandardCopyOption enum which defines how the copy should be done.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

void fileCopyUsingNIOFilesClass() throws IOException 
{
	Path source = Paths.get("c:/temp/testoriginal.txt");
	Path destination = Paths.get("c:/temp/testcopied.txt");

	Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
}

Java Copy File 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.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

private static void fileCopyUsingNIOChannelClass() throws IOException 
{
	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();
}

Java Copy File using Apache Commons IO

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.7</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.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

void fileCopyUsingApacheCommons() throws IOException 
{
	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));
}

Java Copy File using Guava

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>28.2-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.

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

import com.google.common.io.Files;

void fileCopyUsingGuava() throws IOException 
{
	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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java Copy File”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.