Java io stream-Merge stream

Source: Internet
Author: User

2017-11-05 20:15:28

    • Sequenceinputstream

SequenceinputStream:SequenceInputStreamRepresents a logical concatenation of other input streams. It starts with an ordered collection of input streams and reads from the first input stream until the end of the file is reached, then reads from the second input stream, and so on, until the end of the file containing the last input stream is reached.

* Construction Method

* Common Methods

public class Demo6 {public    static void Main (string[] args) throws IOException {        InputStream in1 = new FILEINPUTST Ream ("E:/text.txt");        InputStream in2 = new FileInputStream ("E:/text2.txt");        Sequenceinputstream sis = new Sequenceinputstream (in1, in2);        Bufferedoutputstream bs = new Bufferedoutputstream (New FileOutputStream ("" +                "E:/copy.txt"));        byte[] bys = new byte[1024];        int len = 0;        while (len = Sis.read (bys))! =-1) {            bs.write (bys, 0, Len);        }        Bs.close ();        Sis.close ();    }}

What if there are more than two of them? In fact, the class also provides another construction method: public Sequenceinputstream(enumeration<? extends inputstream> e)

Here the enumeration enumerator is the return value of a method of the vector class: publicenumeration<e> elements()

The rest is very simple:

public class Demo6 {public    static void Main (string[] args) throws IOException {        vector<inputstream> v = new Vector<> ();        InputStream in1 = new FileInputStream ("E:/text.txt");        InputStream in2 = new FileInputStream ("E:/text2.txt");        InputStream in3 = new FileInputStream ("E:/text3.txt");                V.add (in1);        V.add (in2);        V.add (in3);        Enumeration<inputstream> enumeration = V.elements ();        Sequenceinputstream sis = new Sequenceinputstream (enumeration);        Bufferedoutputstream bs = new Bufferedoutputstream (New FileOutputStream ("" +                "E:/copy.txt"));        byte[] bys = new byte[1024];        int len = 0;        while (len = Sis.read (bys))! =-1) {            bs.write (bys, 0, Len);        }        Bs.close ();        Sis.close ();    }}

Java io stream-merge stream

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.