4 ways to copy Java files _java

Source: Internet
Author: User
Tags file copy

Although Java provides an IO action class that can handle files, there is no way to copy the files. Copying a file is an important operation when your program has to deal with a lot of files related to it. However, there are several ways to do Java file copy operations, and here are the most popular ways of 4.

1. Using FileStreams replication

This is the classic way to copy the contents of one file to another file. Use FileInputStream to read bytes of file A and write to file B using FileOutputStream. This is the code for the first method:

private static void Copyfileusingfilestreams (file source, file dest)
    throws IOException {  
  InputStream input = nul l;  
  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 on the try data, so this should be a inefficient, next way we will see the new way.

2. Using FileChannel replication

Java NiO includes the Transferfrom method, which should be faster to copy than file streams. This 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 to use the Apache Commons Fileutils class when you have used your project. Basically, this class uses the 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. Copy using Java7 's files class

If you have some experience in Java 7 You may know that you can copy files from one file to another using the Copy method's file class. 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. Test

Now seeing which of these methods is more efficient, we will replicate a large file using each in a simple program. From caching 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 FileStreams long start = System.nanotime ();
    Long end;
    Copyfileusingfilestreams (source, dest);
 
    System.out.println ("Time taken by filestreams Copy =" + (System.nanotime ()-start);
    Copy files using Java.nio.FileChannel Source = 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));
    The copy file using Java 7 Files class Source = 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 io Source = 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[1024];
      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.co
  Pyfile (source, dest);


 }
 
}

Output:

Time taken by filestreams copy = 127572360 time
taken by filechannels copy = 10449963 time
taken by Java7 Files Co PY = 10808333 time
taken by Apache Commons IO Copy = 17971677


As you can see Filechannels copying large files is the best way. If you deal with larger files, you will notice a greater speed difference. This is an example that shows four different ways to copy a file in Java.

The above is the entire content of this article, I hope to help you learn.

Related Article

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.