The project needs to read and write large files, the results of the survey test made me decide to use Mappedbytebuffer and related classes for file operation, the effect is not generally high.
A lot of online reference resources, the following two is very good:
1. Randomaccessfile class for high-efficiency I/O with 1K memory
2. I/O performance comparison of stream and memory-mapped file in Java
Summary:
1, randomaccessfile itself without buffer read and write, and FileInputStream, FileOutputStream, and so on, directly by byte read and write, performance is unacceptable;
2, the use of Mappedbytebuffer read and write, of course, performance will be greatly improved; in fact, as long as their own buffer, performance will have a very large increase, such as the following two ways (see Code) in the first use of Mappedbytebuffer, the second self-buffering, For 12M files, the latter is even more efficient than the former.
3, bufferedxxxx, such as buffer stream, if only using the default buffer size, performance is not necessarily optimal, to weigh the different circumstances of various factors set the size.
PackageHelloword.helloword;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.RandomAccessFile;ImportJava.nio.MappedByteBuffer;ImportJava.nio.channels.FileChannel; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub } Public voidTest ()throwsIOException {/** Test results are related to buffer size*/ //1. Using mappedbytebuffer:0.7sString srcfile = "D://noname1.txt"; String DestFile= "D://copy.txt"; Randomaccessfile Rafi=NewRandomaccessfile (Srcfile, "R"); Randomaccessfile Rafo=NewRandomaccessfile (DestFile, "RW"); FileChannel FCI=Rafi.getchannel (); FileChannel FCO=Rafo.getchannel (); LongSize =fci.size (); Mappedbytebuffer Mbbi= Fci.map (FileChannel.MapMode.READ_ONLY, 0, size); Mappedbytebuffer Mbbo= Fco.map (FileChannel.MapMode.READ_WRITE, 0, size); LongStart =System.currenttimemillis (); for(inti = 0; i < size; i++) { byteb =Mbbi.get (i); Mbbo.put (i, b); } fci.close (); Fco.close (); Rafi.close (); Rafo.close (); System.out.println ("Spend:" + (Double) (System.currenttimemillis ()-start)/+ + "s"); } Public voidTest2 ()throwsIOException {//2. Handling buffer (randomaccessfile): 0.13sString srcfile = "D://noname1.txt"; String DestFile= "D://copy.txt"; Randomaccessfile Rafi=NewRandomaccessfile (Srcfile, "R"); Randomaccessfile Rafo=NewRandomaccessfile (DestFile, "RW"); byte[] buf =New byte[1024 * 8]; LongStart =System.currenttimemillis (); intc =Rafi.read (BUF); while(C > 0) { if(c = =buf.length) {rafo.write (BUF); } Else{rafo.write (buf,0, c); } C=Rafi.read (BUF); } rafi.close (); Rafo.close (); System.out.println ("Spend:" + (Double) (System.currenttimemillis ()-start)/+ + "s"); } Public voidTest3 ()throwsIOException {//3, bufferedinputstream&bufferedoutputstream:3.02sString srcfile = "D://noname1.txt"; String DestFile= "D://copy.txt"; FileInputStream Rafi=NewFileInputStream (srcfile); FileOutputStream Rafo=NewFileOutputStream (destfile); Bufferedinputstream bis=NewBufferedinputstream (Rafi, 8192); Bufferedoutputstream Bos=NewBufferedoutputstream (Rafo, 8192); LongSize =rafi.available (); LongStart =System.currenttimemillis (); for(inti = 0; i < size; i++) { byteB = (byte) Bis.read (); Bos.write (b); } rafi.close (); Rafo.close (); System.out.println ("Spend:" + (Double) (System.currenttimemillis ()-start)/+ + "s"); }}
Randomacessfile, Mappedbytebuffer, and buffered read/write files