关闭

Java中用内存映射处理大文件

发表于:2012-6-21 09:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:tobacco5648    来源:51Testing软件测试网采编

  在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如下为一个对比实验。

  1. package test;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.RandomAccessFile;  
  7. import java.nio.MappedByteBuffer;  
  8. import java.nio.channels.FileChannel;  
  9. public class Test {  
  10.       
  11.     public static void main(String[] args) {  
  12.         try {  
  13.             FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");  
  14.             int sum=0;  
  15.             int n;  
  16.             long t1=System.currentTimeMillis();  
  17.             try {  
  18.                 while((n=fis.read())>=0){  
  19.                     sum+=n;  
  20.                 }  
  21.             } catch (IOException e) {  
  22.                 // TODO Auto-generated catch block 
  23.                 e.printStackTrace();  
  24.             }  
  25.             long t=System.currentTimeMillis()-t1;  
  26.             System.out.println("sum:"+sum+"  time:"+t);  
  27.         } catch (FileNotFoundException e) {  
  28.             // TODO Auto-generated catch block 
  29.             e.printStackTrace();  
  30.         }  
  31.           
  32.         try {  
  33.             FileInputStream fis=new FileInputStream("/home/tobacco/test/res.txt");  
  34.             BufferedInputStream bis=new BufferedInputStream(fis);  
  35.             int sum=0;  
  36.             int n;  
  37.             long t1=System.currentTimeMillis();  
  38.             try {  
  39.                 while((n=bis.read())>=0){  
  40.                     sum+=n;  
  41.                 }  
  42.             } catch (IOException e) {  
  43.                 // TODO Auto-generated catch block 
  44.                 e.printStackTrace();  
  45.             }  
  46.             long t=System.currentTimeMillis()-t1;  
  47.             System.out.println("sum:"+sum+"  time:"+t);  
  48.         } catch (FileNotFoundException e) {  
  49.             // TODO Auto-generated catch block 
  50.             e.printStackTrace();  
  51.         }  
  52.           
  53.         MappedByteBuffer buffer=null;  
  54.         try {  
  55.             buffer=new RandomAccessFile("/home/tobacco/test/res.txt","rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 01253244);  
  56.             int sum=0;  
  57.             int n;  
  58.             long t1=System.currentTimeMillis();  
  59.             for(int i=0;i<1253244;i++){  
  60.                 n=0x000000ff&buffer.get(i);  
  61.                 sum+=n;  
  62.             }  
  63.             long t=System.currentTimeMillis()-t1;  
  64.             System.out.println("sum:"+sum+"  time:"+t);  
  65.         } catch (FileNotFoundException e) {  
  66.             // TODO Auto-generated catch block 
  67.             e.printStackTrace();  
  68.         } catch (IOException e) {  
  69.             // TODO Auto-generated catch block 
  70.             e.printStackTrace();  
  71.         }  
  72.     }  
  73. }

  测试文件为一个大小为1253244字节的文件。测试结果:

  1. sum:220152087 time:1464  
  2. sum:220152087 time:72  
  3. sum:220152087 time:25

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号