A good memory is better than a bad pen. 5-java Quick File Copy

Source: Internet
Author: User
Tags file copy

If you use a file system such as Luncene or Hadoop, there are a large number of index files to be distributed, you can use ready-made distribution tools, or you can write your own program for quick file copy;

Use NIO make quick copies of files

Package com.daily;

Import Java.io.File;

Import Java.io.FileInputStream;

Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.nio.channels.FileChannel;

/**

*nio fast file copy, maxcount=128m, copy 351M files, 11001 (MS)

 * the same file, with (*1024 * 1024x768)-(18192 * 1024x768), time-consuming (milliseconds)

* @author Fan Fangming

*/

public class Easyfastcopy {

Publicvoid fileCopy (string in, string out) throws IOException {

Filechannelinchannel = new FileInputStream (new File (in)). Getchannel ();

Filechanneloutchannel = new FileOutputStream (new File). Getchannel ();

try{

            // If the memory is large, this value can be expanded appropriately, and there are advantages to copying large files .

Intmaxcount = (128 * 1024 * 1024);

Intmaxcount = (8 * 1024 * 1024);

int MaxCount = (64 * 1024 * 1024)-(32 *1024);

Longsize = Inchannel.size ();

longposition = 0;

while (position < size) {

position+= Inchannel

. TransferTo (Position,maxcount, Outchannel);

}

}finally {

if (Inchannel! = null) {

Inchannel.close ();

}

if (Outchannel! = null) {

Outchannel.close ();

}

}

}

publicstatic void Main (string[] args) throws Exception {

Longstart = System.currenttimemillis ();

Easyfastcopycopy = new Easyfastcopy ();

Copy.filecopy ("D:/ffm83/big.rar", "D:/ffm83/temp/big.rar");

Longend = System.currenttimemillis ();

System.out.println (" Fast File copy time:" + (End-start) + "( ms)");

}

}

A good memory is better than a bad pen. 5-java Quick File Copy

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.