1. 程式人生 > >Java讀取1G以上的txt檔案,並對內容進行解析,利用BufferedReader設定快取大小

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);
	}