Java implements file copy

Source: Internet
Author: User

After a set of simple tests, we found that the file memory ing method provided by java nio achieves the fastest file copy speed, whether large files or small files, in particular, the copy speed of large files is 20 times higher than that of normal methods. The only premise is that the memory needs to be large enough. Otherwise, the file ing must fail. Of course, you can avoid this by splitting files and partially ing, but it is quite troublesome). Second, NIO provides a good file pipeline transmission speed. If you cannot do File Memory ing, we recommend this copy method. In addition, the Buffer size, there is still an impact on the read/write speed. The larger the Buffer, the faster the read/write. allocateDirec () Efficiency improvement is not obvious). Finally, it seems that NIO is more efficient than the old IO. No matter which method is used, the old IO can only read and write one byte of bytes, NIO reads and writes data in blocks relatively fast. Therefore, NIO is recommended when there is no special requirement, currently, NIO can basically cover all functions of old IO. Of course, NIO also provides N new functions ).


  • Test Environment

Eclipse (Juno) JVM (Sun JDK1.7) parameters:-Xms1536m-Xmx1536m-Xverify: none-XX: + UseParallelGC-XX: PermSize = 128M-XX: MaxPermSize = 128MOS parameter: Win7 64Bit + 4 GB physical disk space is sufficient


  • Test code

Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. randomAccessFile; import java. nio. byteBuffer; import java. nio. mappedByteBuffer; import java. nio. channels. fileChannel; public class FileCopy {private static final int BUFFER_SIZE_1024 = 1024; private static final int BUFFER_SIZE_4096 = 4096; private static final int amount = 10240; private static final String FROM_FILE_42MB = "G: /from_42MB.rar "; private static final String FROM_FILE_1GB =" G:/from_350MB.rar "; private static int BUFFER_SIZE = BUFFER_SIZE_1024; private static String FROM_FILE = FROM_FILE_42MB; /*** @ param args * @ throws Exception */public static void main (String [] args) throws Exception {System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "------------"); testFileCopy (); BUFFER_SIZE = BUFFER_SIZE_4096; System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "------------"); testFileCopy (); BUFFER_SIZE = BUFFER_SIZE_10240; System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "--------------"); testFileCopy (); BUFFER_SIZE = BUFFER_SIZE_1024; FROM_FILE = FROM_FILE_1GB; System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "--------------"); testFileCopy (); BUFFER_SIZE = BUFFER_SIZE_4096; FROM_FILE = FROM_FILE_1GB; System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "--------------"); testFileCopy (); BUFFER_SIZE = BUFFER_SIZE_10240; FROM_FILE = FROM_FILE_1GB; System. out. println ("File:" + FROM_FILE + "---- Buffer Size:" + BUFFER_SIZE + "--------------"); testFileCopy ();} private static void testFileCopy () throws FileNotFoundException, IOException {coypByMbb (); copyByNioTransferFrom (); Response (); coypByBufferRead (); coypByFastBufferRead (); coypByStream (); // Old IO style}/*** use FileChannel. transferFrom () implementation * @ throws FileNotFoundException * @ throws IOException */private static void copyByNioTransferFrom () throws FileNotFoundException, IOException {long startTime = System. currentTimeMillis (); RandomAccessFile fromFile = new RandomAccessFile (FROM_FILE, "rw"); FileChannel fromChannel = fromFile. getChannel (); RandomAccessFile toFile = new RandomAccessFile ("G:/to1.rar", "rw"); FileChannel toChannel = toFile. getChannel (); long position = 0; long count = fromChannel. size (); toChannel. transferFrom (fromChannel, position, count); long endTime = System. currentTimeMillis (); System. out. println ("copyByNioTransferFrom time consumed (buffer size no effect):" + (endTime-startTime);}/*** use FileChannel. transferTo () implementation * @ throws FileNotFoundException * @ throws IOException */private static void copyByNioTransferTo () throws FileNotFoundException, IOException {long startTime = System. currentTimeMillis (); RandomAccessFile fromFile = new RandomAccessFile (FROM_FILE, "rw"); FileChannel fromChannel = fromFile. getChannel (); RandomAccessFile toFile = new RandomAccessFile ("G:/to2.rar", "rw"); FileChannel toChannel = toFile. getChannel (); long position = 0; long count = fromChannel. size (); fromChannel. transferTo (position, count, toChannel); long endTime = System. currentTimeMillis (); System. out. println ("copyByNioTransferTo time consumed (buffer size no effect):" + (endTime-startTime);}/*** Use Channel, simple Buffer read/write implementation * @ throws IOException */private static void coypByBufferRead () throws IOException {long startTime = System. currentTimeMillis (); FileInputStream fin = new FileInputStream (FROM_FILE); FileOutputStream fout = new FileOutputStream ("G:/to3.rar"); FileChannel fcin = fin. getChannel (); FileChannel fcout = fout. getChannel (); ByteBuffer buffer = ByteBuffer. allocate (BUFFER_SIZE); while (true) {buffer. clear (); int r = fcin. read (buffer); if (r =-1) {break;} buffer. flip (); fcout. write (buffer);} long endTime = System. currentTimeMillis (); System. out. println ("coypByBufferRead time consumed (buffer size take effect):" + (endTime-startTime ));} /*** implement using Buffer of continuous memory * @ throws IOException */private static void coypByFastBufferRead () throws IOException {long startTime = System. currentTimeMillis (); FileInputStream fin = new FileInputStream (FROM_FILE); FileOutputStream fout = new FileOutputStream ("G:/to4.rar"); FileChannel fcin = fin. getChannel (); FileChannel fcout = fout. getChannel (); ByteBuffer buffer = ByteBuffer. allocateDirect (BUFFER_SIZE); while (true) {buffer. clear (); int r = fcin. read (buffer); if (r =-1) {break;} buffer. flip (); fcout. write (buffer);} long endTime = System. currentTimeMillis (); System. out. println ("coypByFastBufferRead time consumed (buffer size take effect):" + (endTime-startTime ));} /*** use File Memory ing to implement * @ throws IOException */private static void coypByMbb () throws IOException {long startTime = System. currentTimeMillis (); FileInputStream fin = new FileInputStream (FROM_FILE); RandomAccessFile fout = new RandomAccessFile ("G:/to5.rar", "rw"); FileChannel fcin = fin. getChannel (); FileChannel fcout = fout. getChannel (); MappedByteBuffer mbbi = fcin. map (FileChannel. mapMode. READ_ONLY, 0, fcin. size (); MappedByteBuffer mbbo = fcout. map (FileChannel. mapMode. READ_WRITE, 0, fcin. size (); mbbo. put (mbbi); mbbi. clear (); mbbo. clear (); long endTime = System. currentTimeMillis (); System. out. println ("coypByMbb time consumed (buffer size no effect):" + (endTime-startTime ));} /*** use the traditional IO stream read/write Method * @ throws IOException */private static void coypByStream () throws IOException {long startTime = System. currentTimeMillis (); FileInputStream fin = new FileInputStream (FROM_FILE); FileOutputStream fout = new FileOutputStream ("G:/to6.rar"); byte [] buffer = new byte [BUFFER_SIZE]; while (true) {int ins = fin. read (buffer); if (ins =-1) {fin. close (); fout. flush (); fout. close (); break;} else {fout. write (buffer, 0, ins) ;}} long endTime = System. currentTimeMillis (); System. out. println ("coypByStream time consumed (buffer size take effect):" + (endTime-startTime ));}}
  • Test Results

    File :G:/from_42MB.rar ---- Buffer Size : 1024--------------coypByMbb time consumed(buffer size no effect) : 47copyByNioTransferFrom time consumed(buffer size no effect) : 62copyByNioTransferTo time consumed(buffer size no effect) : 47coypByBufferRead time consumed(buffer size take effect) : 249coypByFastBufferRead time consumed(buffer size take effect) : 188coypByStream time consumed(buffer size take effect) : 187         File :G:/from_42MB.rar ---- Buffer Size : 4096--------------coypByMbb time consumed(buffer size no effect) : 15copyByNioTransferFrom time consumed(buffer size no effect) : 16copyByNioTransferTo time consumed(buffer size no effect) : 31coypByBufferRead time consumed(buffer size take effect) : 125coypByFastBufferRead time consumed(buffer size take effect) : 79coypByStream time consumed(buffer size take effect) : 93         File :G:/from_42MB.rar ---- Buffer Size : 10240--------------coypByMbb time consumed(buffer size no effect) : 16copyByNioTransferFrom time consumed(buffer size no effect) : 32copyByNioTransferTo time consumed(buffer size no effect) : 31coypByBufferRead time consumed(buffer size take effect) : 78coypByFastBufferRead time consumed(buffer size take effect) : 62coypByStream time consumed(buffer size take effect) : 63         File :G:/from_350MB.rar ---- Buffer Size : 1024--------------coypByMbb time consumed(buffer size no effect) : 280copyByNioTransferFrom time consumed(buffer size no effect) : 453copyByNioTransferTo time consumed(buffer size no effect) : 7785coypByBufferRead time consumed(buffer size take effect) : 8144coypByFastBufferRead time consumed(buffer size take effect) : 7068coypByStream time consumed(buffer size take effect) : 8503         File :G:/from_350MB.rar ---- Buffer Size : 4096--------------coypByMbb time consumed(buffer size no effect) : 1904copyByNioTransferFrom time consumed(buffer size no effect) : 5978copyByNioTransferTo time consumed(buffer size no effect) : 7379coypByBufferRead time consumed(buffer size take effect) : 7621coypByFastBufferRead time consumed(buffer size take effect) : 7474coypByStream time consumed(buffer size take effect) : 8200         File :G:/from_350MB.rar ---- Buffer Size : 10240--------------coypByMbb time consumed(buffer size no effect) : 328copyByNioTransferFrom time consumed(buffer size no effect) : 6864copyByNioTransferTo time consumed(buffer size no effect) : 7021coypByBufferRead time consumed(buffer size take effect) : 7199coypByFastBufferRead time consumed(buffer size take effect) : 7941coypByStream time consumed(buffer size take effect) : 7801


This article is from "the power comes from the sincere love !" Blog, please be sure to keep this source http://stevex.blog.51cto.com/4300375/1272956

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.