Java IO operations: Merging streams

Source: Internet
Author: User

Merge streams: Merge two files together.

The main operation is the content.

Defined:

 Public class Sequenceinputstreamextends InputStream

Method Summary:

Operation Steps:

1, set up two file InputStream instance objects, respectively, to output the specified two files.

2, set up a file OutputStream instance, which is used to merge and write two files.

2, through the Sequenceinputstream class, the contents of the two files are combined and placed into this class instance stream.

3, read the contents of the Sequenceinputstream instance and put it in a third file.

Example code:

 Packageclass set;ImportJava.io.File;ImportJava.io.SequenceInputStream;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream; Public classsequencedemo{ Public Static voidMain (String args[])throwsException {//all exceptions ThrownInputStream is1 =NULL;//input stream 1InputStream Is2 =NULL;//input stream 1OutputStream OS =NULL;//output StreamSequenceinputstream sis =NULL;//Merging flowsIs1 =NewFileInputStream ("D:" + File.separator + "A.txt") ; Is2=New FileInputStream("D:" + File.separator + "B.txt") ; OS=New FileOutputStream("D:" + File.separator + "Ab.txt") ; SIS=NewSequenceinputstream (IS1,IS2);//instantiating a merge stream        inttemp = 0;//Receive content         while(Temp=sis.Read ()) {!=-1) {//Loop OutputOs.Write(temp);//Save Content} sis.close (); //Close Merge StreamIs1.close ();//turn off input stream 1 'Is2.close ();//turn off input stream 2Os.close ();//turn off the output stream    }};

Operation Result:

The contents of a, B, are incorporated into AB.

Java IO operations: Merging streams

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.