Last Updated:

How do I copy a file to Java? 4 Ways - Examples and Code

Copy file to java

There is no method in the java.io.File class to copy a file from one location to another. Therefore, in this article, we will look at a few of the most commonly used ways to copy a file.

Method 1: Use streams to copy the file

 

This method is the most used. Its essence is as follows:

We create two files (what to copy and where to copy the file). Then create from the source and write it to the resulting copy file using .

InputStreamOutputStream

The following is a Java method for copying a file using streams:

private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}

Method 2: Use FileChannel

You can also use the . There is a very useful method in this class, which is also very often used to copy files. According to the documentation, this method of copying a file is faster than using streams ().java.nio.channels.FileChanneltransferFrom()InputStream,OutputStream

The following is a method for copying files using the class:FileChannel

private static void copyFileUsingChannel(File source, File dest) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{
sourceChannel.close();
destChannel.close();
}
}

Method 3: Use Apache Commons IO

There is a great class with a method that can be used to copy a file to Java. If you are already using in your project, then it makes sense to use the method for simplicity and convenience of the code. Internally, it uses , providing the programmer with only a convenient shell.

Apache Commons IOFileUtilscopyFile(File srcFile, File destFile)Apache Commons IOcopyFile(File srcFile, File destFile)NIO FileChannel

The following is an example of how to copy files in Java.Apache Commons IO

The class was introduced in Java 7 and provides a method for copying a file. It uses the built-in capabilities of the system to copy the file.Filescopy()

Which method of copying a file in Java is the best?

So which method of copying a file in Java is the fastest and most efficient? Different 1Gb files were used for the test.

I decided to test the performance of each method of copying a file and wrote this class:

The result of the program:

Judging by the results of the program, it can be seen that the best way to copy a file in Java is to use threads, that is, the first method in our collection.

It should be noted that during the development of different projects in Java, it is still desirable to use different methods of copying files. The fact is that much depends on the number of files and their size. In our case, we tested 1Gb files.