java 大檔案拆分成小檔案__資料

來源:互聯網
上載者:User
因為涉及到讀取大的檔案的資料,但是程式記憶體太小讀不出來的原因,所以將檔案進行拆分進行讀取

package cn.jado.ctt_check.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.jado.ctt_check.util.IOUtil;

public class SpiltFile {
private static final Logger logger = Logger.getLogger(SpiltFile.class);

public static void main(String[] args) {    String path = "F:/usr/local/統計/utf-8_20170221_wechat_article.DATA";    readFileByLineToList(path, "utf-8");}/*** * 將一個大檔案以1百萬一個檔案進行拆分 * @author jado * @param fileName * @param charsetName * @return */public static  List<String> readFileByLineToList(String fileName,String charsetName){    List<String> lineList = new ArrayList<String>();    String path  =  IOUtil.PathParser(fileName)[0]+"/wechat_msg/";    IOUtil.mkDirs(path);    if(charsetName == null){        charsetName = System.getProperty("file.encoding");    }    BufferedReader  br = null;    InputStreamReader isReader = null;    try {        if(charsetName == null){            charsetName = System.getProperty("file.encoding");        }        isReader  = new InputStreamReader(new FileInputStream(fileName),charsetName);        br = new BufferedReader(isReader);        String tempString = null;        int v  = 0;        while((tempString = br.readLine())!= null){            v+=1;            //  百萬條資料分成一個檔案            lineList.add(tempString);            if(v%100000==0){                for(String line :lineList){                    IOUtil.writeFile(path+v+"_wechat_article.DATA", line+"\n", "utf-8",true);                }                lineList.clear();            }        }    } catch (UnsupportedEncodingException e1) {        // TODO Auto-generated catch block        logger.error("解析檔案編碼異常",e1);                } catch (FileNotFoundException e2) {        // TODO Auto-generated catch block        logger.error("檔案沒有找到異常",e2);    } catch (IOException e3) {        // TODO Auto-generated catch block        logger.error("Io操作異常",e3);    }finally{        if(br != null){            try {                br.close();                br = null;            } catch (IOException e4) {                // TODO Auto-generated catch block                logger.error("檔案關閉異常",e4);            }        }        if(isReader != null){            try {                isReader.close();            } catch (IOException e5) {                // TODO Auto-generated catch block                logger.error("檔案關閉異常",e5);            }        }    }    return lineList;}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.