java-splitting large files (split by row)

Source: Internet
Author: User

 PackageCom.testsplitbynumber;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.nio.charset.Charset;ImportJava.util.HashMap;ImportJava.util.Map;ImportCom.csvreader.CsvReader; Public classSplitfile { Public Static voidMain (String args[]) {string path= "F:/zh.csv"; String folder= path.substring (0, Path.lastindexof ("."))); File File=NewFile (path); File file2=NewFile (folder); Map<String,BufferedWriter> map =NewHashmap<string, bufferedwriter>(); if(!file.exists ()) {System.out.println ("File does not exist!"); }Else {            if(! file2.exists () | | |!file2.isdirectory ())            {File2.mkdir (); }            LongStart =System.currenttimemillis (); Csvreader Reader=NULL ; BufferedWriter BW=NULL; Try{Reader=NewCsvreader (Path, ', ', Charset.forname ("Utf-8")));                Reader.readheaders (); intCount = 0;  while(Reader.readrecord ()) {count++; String strs[]=reader.getvalues (); String Line= Strs[strs.length-1]; String FileName= folder + File.separator + file.getname (). subsequence (0, File.getname (). LastIndexOf (".")) + Line.charat (Line.length ()-1) + ". csv"; Bw=Map.get (fileName); if(BW = =NULL) {BW=NewBufferedWriter (NewFileWriter (NewFile (fileName));                    } map.put (FileName, BW); Bw.write (WriteLine (STRs)+ "\ r \ n"); if(count% 100000 = = 0) {Bw.flush (); System.out.println ("Already processed:" + Count + "bar"); }                }                LongEnd =System.currenttimemillis (); System.out.println ("Total:" + (End-start)/1000 + "s"); } Catch(IOException e) {e.printstacktrace (); }finally{                if(Reader! =NULL) {reader.close (); Reader=NULL; }                 for(BufferedWriter bww:map.values ()) {Try{Bww.flush ();                    Bww.close (); } Catch(IOException e) {e.printstacktrace (); }                }            }        }    }         Public Staticstring WriteLine (String strs[]) {StringBuffer SB=NewStringBuffer ("");  for(inti = 0;i < strs.length;i++) {Strs[i]= Strs[i].replaceall (",", ","); Strs[i]= Strs[i].replaceall ("\\s*", "" "); Sb.append (Strs[i]+ ","); }        returnSb.substring (0, Sb.length ()-1). toString (); }}

java-splitting large files (split by row)

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.