Java operation large File copy

Source: Internet
Author: User
Tags file copy

1. Large file replication can be transmitted using Channel-to-channel in Java NIO, Channel-to-channel transmission can be extremely fast, especially when the underlying operating system provides local support. Some operating systems can transfer data directly without having to pass through the user space. For a lot of data transfer, this would be a huge help.

2. Code

 PackageCom.dingwang.File;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.nio.channels.FileChannel;ImportJava.nio.channels.WritableByteChannel;/*** Class Filetofile.java Implementation Description: TODO class Implementation Description <br> * Write the contents of one file to another file * *@author[email protected] February 5, 2016 morning 11:55:26*/ Public classFiletofile {Private Static Final intDefault_buffer = 3 * 1024; /*** Use channel Copy file * *@paramSource *@paramTarget*/     Public voidTransfer (file source, file target) {FileInputStream in=NULL; FileOutputStream out=NULL; if(!source.exists () | | |Source.isfile ()) {            Throw NewIllegalArgumentException ("File not exsits!"); }        if(Target.exists ()) {target.delete (); }        Try{target.createnewfile (); Inch=NewFileInputStream (source); out=NewFileOutputStream (target); FileChannel Inchannel=In.getchannel (); Writablebytechannel Outchannel=Out.getchannel (); Inchannel.transferto (0, Inchannel.size (), Outchannel);            Inchannel.close ();            Outchannel.close ();            In.close ();        Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }    /*** Traditional input and output stream copy file * *@paramSource *@paramTarget*/     Public voidtransfer2 (file source, file target) {InputStream in=NULL; OutputStream out=NULL; if(!source.exists () | | |Source.isfile ()) {            Throw NewIllegalArgumentException ("File not exsits!"); }        if(Target.exists ()) {target.delete (); }        byte[] buffer =New byte[Default_buffer]; intn = 0; Try{target.createnewfile (); Inch=NewBufferedinputstream (NewFileInputStream (source)); out=NewBufferedoutputstream (NewFileOutputStream (target));  while((n = in.read (buffer))! =-1) {out.write (buffer,0, N);            } out.flush ();            In.close ();        Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}
 PackageCom.dingwang.file;ImportJava.io.File;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.junit.Test;ImportCom.dingwang.File.FileToFile;/*** Class Filetofiletest.java Implementation Description: TODO class Implementation Description * *@author[email protected] February 5, 2016 PM 12:05:58*/ Public classFiletofiletest {//@Test     Public voidFiletofile () {String date=NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ()); String username= System.getproperty ("User.Name"); Filetofile F=NewFiletofile (); File Source=NewFile ("e:\\ production problem Query \\policy_biz_id_2015.dat"); String TargetFileName= "e:\\ production problem query \\target_" + Username + "_" + Date + ". Dat"; File Target=NewFile (TargetFileName); Long Start=System.currenttimemillis ();        F.transfer (source, target); System.out.println ("Time consuming =" + ((System.currenttimemillis ()-start) + "MS"); } @Test Public voidTransfer2 () {String date=NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ()); String username= System.getproperty ("User.Name"); Filetofile F=NewFiletofile (); //e:\\ Production problem query \\policy_biz_id_2015.datFile Source =NewFile ("e:\\ production problem Query \\policy_biz_id_2015.dat"); String TargetFileName= "e:\\ production problem query \\target_" + Username + "_" + Date + ". Dat"; File Target=NewFile (TargetFileName); Long Start=System.currenttimemillis ();        F.transfer2 (source, target); System.out.println ("Time consuming =" + ((System.currenttimemillis ()-start) + "MS"); }}

Compared to the use of channels to copy files than the traditional way of about 1 time times faster, the CPU and memory consumption is also lower, testing the use of 300M files; monitoring JVISUALVM

Java operation large File copy

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.