Summary
Although Java provides an IO operation class that can handle files. However, there is no way to copy a file. Copying files is an important operation when your program has to handle a lot of files related to the time. There are several ways to copy Java files, however, and here are some of the most popular ways to do this in 4. 1. Copying using file Streams
This is the most classic way to copy the contents of a file to another file. Use FileInputStream to read the bytes of file a, using FileOutputStream to write to file B. This is the code for the first method:
public static void Copyfileusingfilestreams (File source,file dest)
Throws ioexception{
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream (source);
Output = new FileOutputStream (dest);
byte[] buf = new byte[1024];
int bytesread;
while ((Bytesread = Input.read (buf)) > 0) {
Output.write (Buf,0,bytesread);
}
}finally {
Input.close ();
Output.close ();
}
}
As you can see, we perform several read and write operations to try the data, so this should be a inefficient one, and the next method we will see is the new way.
2. Copy using FileChannel
Java NiO includes the Transferfrom method, which should be faster to copy than file stream according to the document. Here is the code for the second method:
- private static void copyfileusingfilechannels(file source, File Dest) throws IOException {
- FileChannel inputchannel = null;
- FileChannel outputchannel = null;
- try {
- Inputchannel = new FileInputStream (source). Getchannel ();
- Outputchannel = new FileOutputStream (dest). Getchannel ();
- Outputchannel.transferfrom (Inputchannel, 0, Inputchannel.size ());
- } finally {
- Inputchannel.close ();
- Outputchannel.close ();
- }
- }
3. Using commons IO Replication
Apache Commons IO provides a copy file method in its Fileutils class, which can be used to copy a file to another place. It is very handy when you use the Apache Commons Fileutils class when you have used your project. Basically, this class uses Java NIO filechannel inside. This is the code for the third method:
- private static void copyfileusingapachecommonsio(file source, file dest)
- throws IOException {
- Fileutils.copyfile (source, dest);
- }
4. Copying files class using Java7
If you have some experience in Java 7 You might know that you can use the Copy method of the files class file to copy from one file to another. This is the code for the fourth method:
- private static void copyfileusingjava7files(file source, File dest)
- throws IOException {
- Files.copy (Source.topath (), Dest.topath ());
- }
5. Testing
Now see which of these methods is more efficient, we will copy a large file using each one in a simple program. From cache to avoid any performance obviously we will use four different source files and four different target files. Let's take a look at the code:
- import Java.io.File;
- import Java.io.FileInputStream;
- import Java.io.FileOutputStream;
- import java.io.IOException;
- import Java.io.InputStream;
- import Java.io.OutputStream;
- import Java.nio.channels.FileChannel;
- import java.nio.file.Files;
- import org.apache.commons.io.FileUtils;
- public class copyfilesexample {
- Public static void main(string[] args) throws Interruptedexception,
- IOException {
- File Source = New File ("C:\\users\\nikos7\\desktop\\files\\sourcefile1.txt");
- File Dest = new File ("C:\\users\\nikos7\\desktop\\files\\destfile1.txt");
- //Copy file using FileStreamslong start = System.nanotime ();
- long end;
- Copyfileusingfilestreams (source, dest);
- System.out.println ("Time taken by filestreams Copy ="
- + (System.nanotime ()-start));
- //Copy files using Java.nio.FileChannelsource = new File("C:\\users\\nikos7\\desktop \\files\\sourcefile2.txt ");
- Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile2.txt");
- Start = System.nanotime ();
- Copyfileusingfilechannels (source, dest);
- End = System.nanotime ();
- System.out.println ("Time taken by filechannels Copy =" + (End-start));
- //Copy file using Java 7 Files classSource = new file("c:\\users\\nikos7\\desktop\\ Files\\sourcefile3.txt ");
- Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile3.txt");
- Start = System.nanotime ();
- Copyfileusingjava7files (source, dest);
- End = System.nanotime ();
- System.out.println ("Time taken by Java7 Files Copy =" + (End-start));
- //Copy files using Apache commons iosource = new File("c:\\users\\nikos7\\desktop\\ Files\\sourcefile4.txt ");
- Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile4.txt");
- Start = System.nanotime ();
- Copyfileusingapachecommonsio (source, dest);
- End = System.nanotime ();
- System.out.println ("Time taken by Apache Commons IO Copy ="
- + (End-start));
- }
- private static void copyfileusingfilestreams(file source, File dest)
- throws IOException {
- InputStream input = null;
- OutputStream output = null;
- try {
- input = new FileInputStream (source);
- Output = new FileOutputStream (dest);
- byte[] buf = new byte[1024x768];
- int bytesread;
- While ((bytesread = Input.read (buf)) > 0) {
- Output.write (buf, 0, bytesread);
- }
- } finally {
- Input.close ();
- Output.close ();
- }
- }
- private static void copyfileusingfilechannels(file source, File dest)
- throws IOException {
- FileChannel inputchannel = null;
- FileChannel outputchannel = null;
- try {
- Inputchannel = new FileInputStream (source). Getchannel ();
- Outputchannel = new FileOutputStream (dest). Getchannel ();
- Outputchannel.transferfrom (Inputchannel, 0, Inputchannel.size ());
- } finally {
- Inputchannel.close ();
- Outputchannel.close ();
- }
- }
- private static void copyfileusingjava7files(file source, File dest)
- throws IOException {
- Files.copy (Source.topath (), Dest.topath ());
- }
- private static void copyfileusingapachecommonsio(file source, file dest)
- throws IOException {
- Fileutils.copyfile (source, dest);
- }
- }
Output:
- Time taken by filestreams Copy = 127572360
- Time taken by filechannels Copy = 10449963
- Time taken by Java7 Files Copy = 10808333
- Time taken by Apache Commons IO Copy = 17971677
As you can see, filechannels copying large files is the best method. If you deal with larger files, you will notice a greater speed difference. This is an example that shows four different ways in Java that you can copy a file.
Java File Replication