用java内存映射实现读取文件行(readline)

Java代码   收藏代码
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.nio.CharBuffer;  
  8. import java.nio.MappedByteBuffer;  
  9. import java.nio.channels.FileChannel;  
  10. import java.nio.charset.Charset;  
  11. import java.nio.charset.CharsetDecoder;  
  12. import java.util.Scanner;  
  13. import java.util.zip.GZIPInputStream;  
  14.   
  15. public class Test {  
  16.     static String path = "D:\\log\\proclog\\loganalysis\\0108161331_CHN-MY-1_1021235501.log";  
  17.   
  18.     public static void main(String[] s) throws IOException {  
  19.         stream();  
  20.         mem();  
  21.     }  
  22.   
  23.     public static void stream() throws FileNotFoundException, IOException {  
  24.         Long startTime = System.currentTimeMillis();  
  25.         BufferedReader reader = getReader(new File(path));  
  26.   
  27.         String line;  
  28.         while ((line = reader.readLine()) != null) {  
  29.             // 空转  
  30.         }  
  31.         Long estimatedTime = System.currentTimeMillis() - startTime;  
  32.         System.out.printf("stream Diff: %d ms\n", estimatedTime);  
  33.   
  34.     }  
  35.   
  36.     public static BufferedReader getReader(File f) throws FileNotFoundException, IOException {  
  37.         BufferedReader reader = null;  
  38.         if (f.getName().endsWith(".gz")) {  
  39.             reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(f))));  
  40.         } else {  
  41.             reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));  
  42.         }  
  43.         return reader;  
  44.     }  
  45.   
  46.     public static void mem() throws IOException {  
  47.         Long startTime = System.currentTimeMillis();  
  48.         FileChannel fc = new FileInputStream(path).getChannel();  
  49.         MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());  
  50.         //Charset charset = Charset.forName("US-ASCII");  
  51.         Charset charset = Charset.forName("iso-8859-1");  
  52.         CharsetDecoder decoder = charset.newDecoder();  
  53.         CharBuffer charBuffer = decoder.decode(byteBuffer);  
  54.         Scanner sc = new Scanner(charBuffer).useDelimiter(System.getProperty("line.separator"));  
  55.         while (sc.hasNext()) {  
  56.             sc.next();  
  57.         }  
  58.         fc.close();  
  59.         Long estimatedTime = System.currentTimeMillis() - startTime;  
  60.         System.out.printf("mem Diff: %d ms", estimatedTime);  
  61.     }  
  62. }  

输出:
stream Diff: 147 ms
mem Diff: 2470 ms

 

从输出来看流方式要远远快于内存映射读取,看来逐行读取文本还是继续使用steam api吧。

 

PS. 测试文件大小23MB,使用一个100MB的文件,mem方式报内存溢出,有点尴尬,先做个记号吧。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值