Stream streams Bulk Read and merge files

Source: Internet
Author: User

ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.StringWriter;ImportJava.nio.channels.FileChannel;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.lang.StringUtils;ImportCom.ist.common.properties.SysConfig;/*** File Merge * * **/ Public classServertemplateextendsTemplate {/**cross-platform line break*/     Public Static FinalString line_separator = System.getproperty ("Line.separator"); /*** Merge Documents * @Method Mergefiles * @Author lbh * @Date 2014-12-11 pm 02:59:54 * @paramoutFile path + output file name *@paramfiles input File list*/     Public BooleanMergefiles (String outFile, list<string>files) {         BooleanMergeresult =true; FileChannel Fileoutchannel=NULL;//declaring channelsFileReader FileReader =NULL; BufferedReader BufferedReader=NULL ; Try{            if(Files = =NULL|| Files.size () < 1) {Logger.info ("Make sure there has outnumber 2 files"); //System.out.println ("Make sure there has outnumber 2 files");Mergeresult =false; }            Else if(files.size () = = 1) {//only one file, so just rename itFile File =NewFile (Files.get (0). toString ()); File.renameto (NewFile (outFile)); }            Else{Fileoutchannel=NewFileOutputStream (OutFile). Getchannel ();//instantiate the output channel, at which point theCreate a file. //Loop All Files                /*Iterator it = Filemap.keyset (). Iterator (); int i = 0; Count while (It.hasnext ()) {*/                 for(inti=0; I<files.size (); i++){                    //System.out.println (Files.get (i));File File =NewFile (Files.get (i). toString ()); if(!file.exists ()) {//Fault tolerance CheckLogger.info ("Make sure every file is existed"); //System.out.println ("file:" + files.get (i) + "is not existed");} filereader=Newfilereader (file); if(!filereader.ready ()) {//Fault tolerance CheckLogger.info ("Make sure every file is existed"); //System.out.println ("file" + files.get (i) + " is not already toRead");} BufferedReader=NewBufferedReader (FileReader); /*InputStreamReader inputstreamreader = new InputStreamReader (new FileInputStream (Files[i]), "UTF-8");//                    This reads the file for stream mode, makes the backup. BufferedReader BufferedReader = new BufferedReader (inputstreamreader);*/                                         //Start processing Files                    if(BufferedReader! =NULL){                        if(i = = 0) {//if it is the first file, then the first two lines are removed before the last row is writtenMergeresult = Handlefile ("fist", BufferedReader, outFile); }                        Else if(i = = Files.size ()-1) {//If this is the last file, then remove the first two lines, keep the last line Mergeresult= Handlefile ("Last", BufferedReader, outFile); }                        Else{//Others remove the first two lines and the last line at the same timeMergeresult = Handlefile ("normal", BufferedReader, outFile); }                        //System.out.println ("file:" + files.get (i) + "merge sate is--"+mergeresult);//logger.info ("File:" +files.get (i) + "merge sate is--"+mergeresult);                    }                                         //Delete the fileFile.delete (); }            }            } Catch(IOException e) {e.printstacktrace (); returnMergeresult; }                         //Close                         if(Fileoutchannel! =NULL){                Try{Bufferedreader.close ();}Catch(IOException Ignore) {returnMergeresult;} }            if(FileReader! =NULL){                Try{Filereader.close ();}Catch(IOException Ignore) {returnMergeresult;} }            if(BufferedReader! =NULL){                Try{Bufferedreader.close ();}Catch(IOException Ignore) {returnMergeresult;} }                 returnMergeresult; }         /*** Process Single file, write immediately after reading, type:fist--first file, last--last file, and other file * @Method handlefile * @Author LBH * @Date 2014-12-11 a.m. 09:49:19*/     Public Static Booleanhandlefile (String type, BufferedReader BufferedReader, String outFile) {BooleanHandleresult =false;//is successfulFileWriter FileWriter =NULL; String readString= "";//storing variables for reading dataString stra,strb,strc;//Temp Variable        Try{FileWriter=NewFileWriter (OutFile,true); if("Fist". Equals (Type)) {//processing of the first fileStra = ""; StrB= ""; STRC= ""; intFisti = 0;//Count                 while((STRC = Bufferedreader.readline ())! =NULL){//start looping from the first lineStra =StrB; StrB=strc; ReadString= Stra + line_separator;//the last line of STRB is discarded//Output                    if(Fisti > 0) {//avoid the first line of blank linesFilewriter.write (readString); } Fisti++; } Handleresult=true; }            if("Last". Equals (Type)) {//processing of end filesStra = "";                Bufferedreader.readline (); Bufferedreader.readline (); //executed two times, the first two rows have been read and discarded.                  while((Stra = Bufferedreader.readline ())! =NULL){//Start Cycle ReadreadString = Stra +Line_separator; //OutputFilewriter.write (readString); } Handleresult=true; }            Else{//processing of other filesStra = ""; StrB= ""; STRC= "";                Bufferedreader.readline (); Bufferedreader.readline (); //executed two times, the first two rows have been read and discarded.                 Longi = 0;  while((STRC = Bufferedreader.readline ())! =NULL){//Start Cycle ReadStra =StrB; StrB=strc; ReadString= Stra + line_separator;//the last line of STRB is discarded//Output                    if(i > 0) {//avoid the first line of blank linesFilewriter.write (readString); } I++; } Handleresult=true; }        }        Catch(IOException e) {e.printstacktrace (); returnHandleresult; }        finally{            Try{bufferedreader.close ();                Filewriter.close (); }            Catch(IOException Ignore) {returnHandleresult;} }                 returnHandleresult; }          Public Static voidMain (string[] args)throwsexception{/*System.out.println ("Start");        list<string> ssss = new arraylist<string> ();        Ssss.add ("E:\\111\\xmltest\\a.xml");        Ssss.add ("E:\\111\\xmltest\\b.xml");        Ssss.add ("E:\\111\\xmltest\\c.xml");        Servertemplate st = new Servertemplate ();        St.mergefiles ("E:\\111\\xmltest\\stream2.xml", ssss); System.out.println ("End");*/    }     }
View Code

Stream streams Bulk Read and merge files

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.