Java merge files

Source: Internet
Author: User

When you want to merge the contents of two files together, the first file writes a file, and then reads the next file in the write, the individual feels very simple, and the implementation code is very easy:)

1  Public voidmergetwofiles (String inpath1,string inpath2,string outpath) {2File out =NewFile (outpath);3         if(!out.exists ()) Out.mkdir ();4 5         Try {6FileReader fr1=NewFileReader (INPATH1);//read the contents of the first file7FileReader fr2=NewFileReader (INPATH2);//read the contents of a second file8BufferedReader br1=NewBufferedReader (FR1);9BufferedReader br2=NewBufferedReader (FR2);TenBufferedWriter bw3=NewBufferedWriter (NewFileWriter (OutputFile)); One String s,s1; As=br1.readline (); -s1=br2.readline (); -              while(s!=NULL) the             { -Bw3.write (s);//write the first file -s=br1.readline (); -Bw3.newline ();//line Break +             } -              while(s1!=NULL) +             { ABw3.write (S1);//write the contents of the second file ats1=br2.readline (); - bw3.newline (); -             } - br1.close (); - br2.close (); - bw3.close (); inSystem.out.println ("Merge Succeed ..."); -}Catch(FileNotFoundException e) { to             //TODO auto-generated Catch block + e.printstacktrace (); -}Catch(IOException e) { the             //TODO auto-generated Catch block * e.printstacktrace (); $         }Panax Notoginseng}

Java 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.