When dealing with large files, frequent read and write operations using ordinary FileInputStream or FileOutputStream or randomaccessfile will cause the process to slow down due to frequent read and write external memory. The following is a comparison experiment.
Packagetest; ImportJava.io.BufferedInputStream; ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; Importjava.io.IOException; ImportJava.io.RandomAccessFile; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel; Public classTest { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); intSum=0; intN; Longt1=System.currenttimemillis (); Try { while((N=fis.read ()) >=0) {sum+=N; } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intSum=0; intN; Longt1=System.currenttimemillis (); Try { while((N=bis.read ()) >=0) {sum+=N; } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Mappedbytebuffer Buffer=NULL; Try{buffer=NewRandomaccessfile ("/home/tobacco/test/res.txt", "RW"). Getchannel (). Map (FileChannel.MapMode.READ_WRITE, 0, 1253244); intSum=0; intN; Longt1=System.currenttimemillis (); for(inti=0;i<1253244;i++) {n=0x000000ff&Buffer.get (i); Sum+=N; } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }
The test file is a file with a size of 1253244 bytes. Test results:
sum:220152087 time:1464 sum:220152087 time:72 sum:220152087 time:25
Indicates that the read data is correct. Delete the data processing part thereof.
ImportJava.io.BufferedInputStream; ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; Importjava.io.IOException; ImportJava.io.RandomAccessFile; ImportJava.nio.MappedByteBuffer; ImportJava.nio.channels.FileChannel; Public classTest { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); intSum=0; intN; Longt1=System.currenttimemillis (); Try { while((N=fis.read ()) >=0){ //sum+=n; } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{FileInputStream fis=NewFileInputStream ("/home/tobacco/test/res.txt"); Bufferedinputstream bis=NewBufferedinputstream (FIS); intSum=0; intN; Longt1=System.currenttimemillis (); Try { while((N=bis.read ()) >=0){ //sum+=n; } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Mappedbytebuffer Buffer=NULL; Try{buffer=NewRandomaccessfile ("/home/tobacco/test/res.txt", "RW"). Getchannel (). Map (FileChannel.MapMode.READ_WRITE, 0, 1253244); intSum=0; intN; Longt1=System.currenttimemillis (); for(inti=0;i<1253244;i++){ //N=0x000000ff&buffer.get (i); //sum+=n; } LongT=system.currenttimemillis ()-T1; System.out.println ("Sum:" +sum+ "Time:" +t); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }
Test results:
sum:0 time:1458 sum:0 time:67 sum:0 time:8
This shows that some or all of the files are mapped to memory after reading and writing, the speed will be much higher.
This is because the memory-mapped file first maps the file on the external memory to a contiguous area in memory, is treated as a byte array, the read-write operation directly to the memory, and then re-map the memory area to the external memory file, which saves the intermediate time to read and write, greatly reducing the read and write time.
Java uses memory mapping for large file uploads