2017-11-05 20:15:28
SequenceinputStream:SequenceInputStream
Represents 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