在處理大文件時,若是利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 來進行頻繁的讀寫操做,都將致使進程因頻繁讀寫外存而下降速度.以下爲一個對比實驗。java
package test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) { try { FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); int sum=0; int n; long t1=System.currentTimeMillis(); try { while((n=fis.read())>=0){ sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); BufferedInputStream bis=new BufferedInputStream(fis); int sum=0; int n; long t1=System.currentTimeMillis(); try { while((n=bis.read())>=0){ sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } MappedByteBuffer buffer=null; try { buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); int sum=0; int n; long t1=System.currentTimeMillis(); for(int i=0;i<1253244;i++){ n=0x000000ff&buffer.get(i); sum+=n; } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
測試文件爲一個大小爲1253244字節的文件。測試結果:數組
sum:220152087 time:1464
sum:220152087 time:72
sum:220152087 time:25
app
說明讀數據無誤。刪去其中的數據處理部分。dom
package test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) { try { FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); int sum=0; int n; long t1=System.currentTimeMillis(); try { while((n=fis.read())>=0){ //sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt"); BufferedInputStream bis=new BufferedInputStream(fis); int sum=0; int n; long t1=System.currentTimeMillis(); try { while((n=bis.read())>=0){ //sum+=n; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } MappedByteBuffer buffer=null; try { buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1253244); int sum=0; int n; long t1=System.currentTimeMillis(); for(int i=0;i<1253244;i++){ //n=0x000000ff&buffer.get(i); //sum+=n; } long t=System.currentTimeMillis()-t1; System.out.println("sum:"+sum+" time:"+t); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
測試結果:學習
sum:0 time:1458
sum:0 time:67
sum:0 time:8
測試
因而可知,將文件部分或者所有映射到內存後進行讀寫,速度將提升不少。spa
這是由於內存映射文件首先將外存上的文件映射到內存中的一塊連續區域,被當成一個字節數組進行處理,讀寫操做直接對內存進行操做,然後再將內存區域從新映射到外存文件,這就節省了中間頻繁的對外存進行讀寫的時間,大大下降了讀寫時間。code
Ps :該文章只是在本人學習中檢索到的相對而言比較專業的文章,可是沒有通過實踐的驗證。orm