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 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 thejava.nio.file.Files
class so it is equivalent to use thejava.nio.file.Files.copy()
function.IOUtils
– It copies bytes from a large (over 2GB)InputStream
to anOutputStream
. This method uses the provided buffer, so there is no need to use aBufferedInputStream
.
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 !!
firstpostcommenter
I am using Java8. Which method is the best one?
Lokesh Gupta
For small files, I will go with
java.nio.file.Files.copy()
. For larger files, I will try first theFileChannel.transferTo()
API.