Java large files split into small files __ data

Source: Internet
Author: User
Because it involves reading data from large files, but the program memory is too small to read the reason, so the file is split to read

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/statistic/utf-8_20170221_wechat_article.
    DATA ";
Readfilebylinetolist (Path, "Utf-8"); /*** * Split a large file into 1,000,001 files * @author Jado * @param fileName * @param charsetname * @return/public static LIST&L T String> readfilebylinetolist (String filename,string charsetname) {list<string> linelist = new ArrayList<St
    Ring> ();
    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;
            Millions data into a file 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.erro            
    R ("Parsing file Encoding exception", E1);
    catch (FileNotFoundException E2) {//TODO auto-generated catch block Logger.error ("File not found exception", E2);
    The catch (IOException E3) {//TODO auto-generated catch block Logger.error ("IO Operation exception", E3);
                }finally{if (br!= null) {try {br.close ();
            br = NULL;
            catch (IOException e4) {//TODO auto-generated catch block Logger.error ("File Shutdown exception", E4); } if (IsreaDer!= null) {try {isreader.close ();
            catch (IOException e5) {//TODO auto-generated catch block Logger.error ("File Shutdown exception", E5);

}} return linelist;
 }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.