Java File Replication

Source: Internet
Author: User

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:

  1. private static void copyfileusingfilechannels(file source, File Dest) throws IOException {
  2. FileChannel inputchannel = null;
  3. FileChannel outputchannel = null;
  4. try {
  5. Inputchannel = new FileInputStream (source). Getchannel ();
  6. Outputchannel = new FileOutputStream (dest). Getchannel ();
  7. Outputchannel.transferfrom (Inputchannel, 0, Inputchannel.size ());
  8. } finally {
  9. Inputchannel.close ();
  10. Outputchannel.close ();
  11. }
  12. }
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:

    1. private static void copyfileusingapachecommonsio(file source, file dest)
    2. throws IOException {
    3. Fileutils.copyfile (source, dest);
    4. }
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:

    1. private static void copyfileusingjava7files(file source, File dest)
    2. throws IOException {
    3. Files.copy (Source.topath (), Dest.topath ());
    4. }
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:

  1. import Java.io.File;
  2. import Java.io.FileInputStream;
  3. import Java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import Java.io.InputStream;
  6. import Java.io.OutputStream;
  7. import Java.nio.channels.FileChannel;
  8. import java.nio.file.Files;
  9. import org.apache.commons.io.FileUtils;
  10. public class copyfilesexample {
  11. Public static void main(string[] args) throws Interruptedexception,
  12. IOException {
  13. File Source = New File ("C:\\users\\nikos7\\desktop\\files\\sourcefile1.txt");
  14. File Dest = new File ("C:\\users\\nikos7\\desktop\\files\\destfile1.txt");
  15. //Copy file using FileStreamslong start = System.nanotime ();
  16. long end;
  17. Copyfileusingfilestreams (source, dest);
  18. System.out.println ("Time taken by filestreams Copy ="
  19. + (System.nanotime ()-start));
  20. //Copy files using Java.nio.FileChannelsource = new File("C:\\users\\nikos7\\desktop \\files\\sourcefile2.txt ");
  21. Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile2.txt");
  22. Start = System.nanotime ();
  23. Copyfileusingfilechannels (source, dest);
  24. End = System.nanotime ();
  25. System.out.println ("Time taken by filechannels Copy =" + (End-start));
  26. //Copy file using Java 7 Files classSource = new file("c:\\users\\nikos7\\desktop\\ Files\\sourcefile3.txt ");
  27. Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile3.txt");
  28. Start = System.nanotime ();
  29. Copyfileusingjava7files (source, dest);
  30. End = System.nanotime ();
  31. System.out.println ("Time taken by Java7 Files Copy =" + (End-start));
  32. //Copy files using Apache commons iosource = new File("c:\\users\\nikos7\\desktop\\ Files\\sourcefile4.txt ");
  33. Dest = New File ("C:\\users\\nikos7\\desktop\\files\\destfile4.txt");
  34. Start = System.nanotime ();
  35. Copyfileusingapachecommonsio (source, dest);
  36. End = System.nanotime ();
  37. System.out.println ("Time taken by Apache Commons IO Copy ="
  38. + (End-start));
  39. }
  40. private static void copyfileusingfilestreams(file source, File dest)
  41. throws IOException {
  42. InputStream input = null;
  43. OutputStream output = null;
  44. try {
  45. input = new FileInputStream (source);
  46. Output = new FileOutputStream (dest);
  47. byte[] buf = new byte[1024x768];
  48. int bytesread;
  49. While ((bytesread = Input.read (buf)) > 0) {
  50. Output.write (buf, 0, bytesread);
  51. }
  52. } finally {
  53. Input.close ();
  54. Output.close ();
  55. }
  56. }
  57. private static void copyfileusingfilechannels(file source, File dest)
  58. throws IOException {
  59. FileChannel inputchannel = null;
  60. FileChannel outputchannel = null;
  61. try {
  62. Inputchannel = new FileInputStream (source). Getchannel ();
  63. Outputchannel = new FileOutputStream (dest). Getchannel ();
  64. Outputchannel.transferfrom (Inputchannel, 0, Inputchannel.size ());
  65. } finally {
  66. Inputchannel.close ();
  67. Outputchannel.close ();
  68. }
  69. }
  70. private static void copyfileusingjava7files(file source, File dest)
  71. throws IOException {
  72. Files.copy (Source.topath (), Dest.topath ());
  73. }
  74. private static void copyfileusingapachecommonsio(file source, file dest)
  75. throws IOException {
  76. Fileutils.copyfile (source, dest);
  77. }
  78. }

Output:

    1. Time taken by filestreams Copy = 127572360
    2. Time taken by filechannels Copy = 10449963
    3. Time taken by Java7 Files Copy = 10808333
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.