Comparison of several replication methods
The copied file is a txt file of 980m
1. FileChannel method
Code:
Public Static voidMappedbuffer ()throwsioexception{Longstart=System.currenttimemillis (); FileChannel Read=NewFileInputStream ("N2.txt"). Getchannel (); FileChannel writer=NewRandomaccessfile ("N5.txt", "RW"). Getchannel (); Longi = 0; LongSize = Read.size ()/30; Bytebuffer BB,CC=NULL; while(I<read.size () && (Read.size ()-i) >size) {BB=Read.map (FileChannel.MapMode.READ_ONLY, I, size); CC=Writer.map (FileChannel.MapMode.READ_WRITE, I, size); Cc.put (BB); I+=size; Bb.clear (); Cc.clear (); } BB= Read.map (FileChannel.MapMode.READ_ONLY, I, read.size ()-i); Cc.put (BB); Bb.clear (); Cc.clear (); Read.close (); Writer.close (); LongEnd=System.currenttimemillis (); System.out.println ("Spents:" + (End-start) + "milliseconds"); }
Time: 807ms
Using Newio technology is the fastest way to replicate large files, especially in this method, which uses memory-mapping technology very quickly.
2. FileInputStream Technology
Public Static voidFileCopy (String srcfile,string tarfile)throwsioexception{Longstart=System.currenttimemillis (); FileInputStream FIS=NULL; FileOutputStream Fos=NULL; File F=NewFile (srcfile); FIS=NewFileInputStream (f); FOS=NewFileOutputStream (Tarfile); intLen=0; byte[] B =New byte[T]; while((Len=fis.read (b))!=-1) {fos.write (b); Fos.flush (); } LongEnd=System.currenttimemillis (); System.out.println ("Spents:" + (End-start) + "milliseconds"); if(fis!=NULL) {fis.close (); } if(fos!=NULL) {fos.close (); } }
Take:
1072ms, the speed is not slow, in the processing of text documents, the traditional IO technology is not slow, but if the image stream file processing, the speed is much slower than the NIO technology.
3, Bufferedoutputstream
More packaging than FileInputStream.
Public Static voidFileCopy2 (String srcfile,string tarfile)throwsioexception{Longstart=System.currenttimemillis (); Bufferedoutputstream Fos=NewBufferedoutputstream (NewFileOutputStream (NewFile (tarfile)); Bufferedinputstream FIS=NewBufferedinputstream (NewFileInputStream (NewFile (srcfile)); intLen=0; byte[] B =New byte[T]; while((Len=fis.read (b))!=-1) {fos.write (b); Fos.flush (); } LongEnd=System.currenttimemillis (); System.out.println ("Spents:" + (End-start) + "milliseconds"); if(fis!=NULL) {fis.close (); } if(fos!=NULL) {fos.close (); } }
Take:
Time consuming ask 1175ms is slower than FileInputStream 100ms (the premise here is that the cache array size is consistent by 100000)
4, BufferedReader
Public Static voidBufferedReader (String srcfile,string tarfile)throwsioexception{Longstart=System.currenttimemillis (); BufferedReader BR=NewBufferedReader (NewFileReader (NewFile (srcfile)); BufferedWriter FR=NewBufferedWriter (NewFileWriter (NewFile (tarfile)); intLen = 0; Char[] ch =New Char[T]; while((Len=br.read (CH))!=-1) {fr.write (CH); } LongEnd=System.currenttimemillis (); System.out.println ("Spents:" + (End-start) + "milliseconds"); Br.close (); Fr.close (); }
Time-consuming to reach 50s, compared to the previous methods is very poor, but this parameter is not the optimal parameter, if you change the size of the array, the speed can be significantly improved
Compared to the previous method is still a long way off.
5, FileReader
Public Static voidBufferedReader2 (String srcfile,string tarfile)throwsioexception{Longstart=System.currenttimemillis (); FileReader BR=NewFileReader (NewFile (srcfile)); FileWriter FR=NewFileWriter (NewFile (tarfile)); intLen = 0; Char[] ch =New Char[T]; while((Len=br.read (CH))!=-1) {fr.write (CH); } LongEnd=System.currenttimemillis (); System.out.println ("Spents:" + (End-start) + "milliseconds"); Br.close (); Fr.close (); }
This method is less than bufferedreader a layer of packaging, faster
Tests have found that the speed of this method is less affected by the size of the array
The parameters of all the methods in this document are not optimal, but the speed between the various methods has a significant gap, it is no longer cumbersome to find the optimal parameters
Java 1G Large File copy