Consolidated stream of Java 21-13 IO streams

Source: Internet
Author: User

: Represents the logical concatenation of other input streams SequenceInputStream .

Construction Method Summary
Sequenceinputstream (enumeration <? Extends Inputstream> e)
           Initializes the newly created Sequenceinputstream by remembering the parameters, which must be a enumeration for the build Run-time type InputStream object The type Parameter.
Sequenceinputstream (inputstream  s1, inputstream s2)
           Initialize the newly created Sequenceinputstream by remembering these two parameters, read the s1 first, and then read S2 to provide a The bytes read by Sequenceinputstream .

The second construction method is shown first:

Sequenceinputstream (inputstream s1, InputStream s2)

Requirements: Copy the contents of 2 files to another 1 files
Previous Actions:
A.txt--b.txt
C.txt--d.txt
Now you want to:
A.txt+b.txt--c.txt

1  public Static voidMain (string[] Args)throwsIOException {2         //merge Datastreamdemo.java and user.txt into Copy.txt3         4         //first, 2 files are encapsulated, because the Sequenceinputstream (inputstream s1, inputstream s2) required parameter list is the InputStream class5         //Fileinputstrem is a subclass of inputstream, using Polymorphism:6InputStream in1 =NewFileInputStream ("datastreamdemo.java");7InputStream in2 =NewFileInputStream ("user.txt");8         //put these two files into the Sequenceinputstream.9Sequenceinputstream in =Newsequenceinputstream (in1, in2);Ten          one         //Package Destination aBufferedoutputstream bw =NewBufferedoutputstream (NewFileOutputStream ("Copy.txt")); -         //reads in1 and in2 data and writes in -         byte[] by =New byte[1024]; the         intLen = 0; -              while(len = In.read (by))! =-1){ -Bw.write (by,0, len); -             }     + in1.close (); - in2.close (); + bw.close (); a in.close (); at          -}

3, demonstrate the first method of Construction:

Sequenceinputstream (enumeration E)
Through a simple review, we know that enumeration is the return value type of a method in a Vector.
Enumeration<e> Elements ()

Requirements: copy data from 3 files to a different file
Previous Actions:
A.txt--b.txt
C.txt--d.txt
E.txt--f.txt
Now you want to:
A.txt+b.txt+c.txt--d.txt

1      public Static voidMain (string[] Args)throwsIOException {2         //Sequenceinputstream (enumeration E) needed is enumeration3         //and enumeration is the return value type of a method in a Vector. enumeration<e> Elements ()4         //so it's going to be the way. 5 6         //first, Define a vector object7Vector<inputstream> v =NewVector<inputstream>();8InputStream I1 =NewFileInputStream ("pw.txt");9InputStream i2 =NewFileInputStream ("user.txt");TenInputStream i3 =NewFileInputStream ("datastreamdemo.java"); one          a         //add these three files to a vector - V.add (i1); - V.add (i2); the V.add (i3); -         //This method of achieving vector, get enumeration -Enumeration<inputstream> e =v.elements (); -         //the following method can be used later, so that the data in the 3 files is stored in the SI +Sequenceinputstream si =NewSequenceinputstream (e); -          +         //Create an input stream aBufferedoutputstream bo =NewBufferedoutputstream (NewFileOutputStream ("Copy.txt")); at          -         //read the Vertor data and add it to the Copy.txt -         byte[] by =New byte[1024]; -         intLen = 0; -          while(len = Si.read (by))! =-1){ -Bo.write (by,0, len); in         } - si.close (); to bo.close (); +}

Consolidated stream of Java 21-13 IO 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.