Although Java provides a IO operation class that can process files, there is no method of copying files. Copy files are an important operation. When your program must handle many files related. However, there are several methods that can be replicated by the Java file, and the most popular methods in 4 are listed below.
1. Copy with FileStreams
This is the most classic way to copy the content of one file to another file. Read the bytes of file A using FileInputStream, and write it to file B with FileoutPutStream. This is the code of the first method:
Private Static Void CopyfileusingFileStreams (File Source, File Dest) Throws IOEXception {InputStream Input = Null; OutputStream Output = NULL; Try {INP UT = 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 (); ();} }As you see a few data of reading and writing operations, this should be a low -efficiency. The next method we will see a new way.
2. Use FileChannel to copy
Java NIO includes the Transferfrom method, which should be faster than the file flow copy according to the document. This is the code of the second method:
Private Static Void CopyfileusingFileChannels (File Source, File Dest) Throws IOEXception {FileChannel InputChannel = Null; try {inputChannel = new fileinputStream (source) .getChannel (); outputChannel = new fileoutputStream (dest) .getChannel (); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { inputChannel.close(); outputChannel.close(); }}3. Use Commons IO to copy
Apache Commons IO provides a copy file method in its FileUtils class, which can be used to copy one file to another place. It is very convenient for you to use your project when using the Apache Commons Fileutils class. Basically, this class uses Java Nio FileChannel. This is the code of the third method:
Private Static Void Copyfileusingapachecommonsio (File Source, File Dest) Throws IOEXCEPTION {FileUtils.copyfile (Source, Dest);}4. Copy with the Files class of Java7
If you have some experience in Java 7, you may know that you can use the copy method of Files files to copy from one file to another file. This is the code of the fourth method:
Private Static Void Copyfileusingjava7files (File Source, File Dest) Throws IOEXception {Files.copy (Source.topath (), Dest.topath ());}5. Test
Now see which one of these methods is more efficient, we will copy a large file to use each simple program. From cache to avoid any performance, we will obviously use four different source files and four different target files. Let's take a look at the code:
Import java.io.file; Import Java.FileInputStream; Import Java.FileoutPutstream; Import java.io.ioxception; Import java.Io.InputStream; Import t 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) Throw s interuPtexception, 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 filestreams long start = system.nanotime (); long end; CopyFileusingFilestreams (source, Dest); System.out.println ("Time taken by filestreams C opy = " + (System.nanotime () - Start)); / /Copy Files user java.nio.FileChannel Source = New File ("C: //users/nikos7//desktop/Files//sourceFile2.txt"); nikos7 // desktop // files // destfile2.txt "); start = system.nanotime (); CopyfileusingFileChannels (source, Dest); End = System.nanotime (); UT.println ("Time taken by FileChannels Copy = " + (End -Start)); // Copy File USING Java 7 Files Class Source = New File (" C: //USERS//nikos7//desktop//sourceFile3.txt "); Dest = New File ("C: //Users//nikos7//desktop/files/destfile3.txt"); Start = System.nanotime (); Time (); System. Out.println ("Time Taken by Java7 Files Copy =" + (END -Start)); // Copy Files USING Apache Commons IO Source = New File ("C: // Nikos7 // Desktop/ /Files/ /SourceFile4.txt "); Dest = New File (" C: //users/nikos7//desktop/files/destfile4.txt "); Start = System.nanotime (); sio (source, destry); end = System.nanotime (); System.out.println ("Time taken by Apache Commons IO Copy =" + (END -Start)); Le 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]; Tonsread; While (bytesream = Input.read (BUF) )> 0) {output.write (buf, 0, bytesRead);}} Finally {input.close (); output.close ();}} Private Static void Copyfilechalechanenels (File so Urce, File Dest) Throws IOEXception {FileChannel InputChannel = Null; FileChannel OutputChannel = NULL; Try {InputChannel = New FileInputStream (Source) .getChannel (); OutputChannel = New Fileoutstream (DEST) .ge tChannel (); OutputChannel.transferfrom (InputChannel, 0, inputChannel.size ());};};} Finally {inputChannel.close (); OutputChannel.close ();}} Private Static Void Copyfileusingjava7files (File Dest) Throws IOEXCEPION 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 = 127572360Time taken by FileChannels Copy = 10449963Time taken by Java7 Files Copy = 1080833333333333333333333333333333333333333333333333time By Apache Commons IO Copy = 17971677
Just as you can see FileChannels copy large files are the best way. If you handle larger files, you will notice a greater speed difference. This is an example. The example demonstrates the four different methods in Java to copy a file.
The above is all the contents of this article. I hope it will be helpful to everyone's learning.