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

读取文件路径 ,读入

使用带缓冲的输入输出流,效率更高,速度更快。创建一个内部缓冲区数组并将其存储在 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 line = "";
    	System.out.println("======开始=====");
    	while((line = reader.readLine()) != null){
    	   //TODO: write your business
			if(!(null!=line&&"".equals(line))){
				String[] dataList = line.split("\\|");
				if(dataList.length>0){
					if(2 == dataList.length){
						countTran=Integer.valueOf(dataList[0]);
					}else if (dataList.length>2){
						String tranType = dataList[3];
						if(type.equals(tranType)){
							typeCount++;
						}
						String tranStatus = dataList[4];
						if("00".equals(tranStatus)||"03".equals(tranStatus)){
							successState++;
						}else{
							errorState++;
						}
					}
				}
			}
    	}
    	System.out.println("======================");
		System.out.println(+countTran);
		System.out.println(successState);
		System.out.println(errorState);
		System.out.println(typeCount);
		System.out.println("======================");
    	System.out.println("======结束=====");
    	long end=System.currentTimeMillis();
		System.out.println(end-start);
	}

猜你喜欢

转载自blog.csdn.net/jellyjiao2008/article/details/84554144