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 ).
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
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 ));}}
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