Java读取1G以上的txt文件,并对内容进行解析,利用BufferedReader设置缓存大小

本文介绍如何使用Java高效地读取1GB以上的txt大文件,通过BufferedReader结合5MB的缓冲区进行内容解析,提升读取速度。
摘要由CSDN通过智能技术生成

读取文件路径 ,读入

使用带缓冲的输入输出流,效率更高,速度更快。创建一个内部缓冲区数组并将其存储在 buf 中,该buf的大小默认为8192。

File file = new File(filepath);   
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));    
BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件  
        

 public static void main(String[] args) throws Exception {
    	long start=System.currentTimeMillis();
    	String filepath="E:/XX/Data/test_1g.txt";
    	File file = new File(filepath);   
    	BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));    
    	BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件  
    	
    	int countTran=0;
		int successState=0;
		int errorState=0;
		int typeCount=0;
		String type="0110";
    	
    	String li
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值