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)