SequenceInputStream usage. correct use of dogs and scissors

Source: Internet
Author: User

SequenceInputStream usage. correct use of dogs and scissors

1 package test; 2 3/** 4 * SequenceInputStream merge streams. Combine the connected stream sets into an input stream and read from the first input stream, until the end of the file is reached, then read from the second input stream, and so on until the end of the last input stream is reached. Merging streams combines multiple sources into one source. It can receive multiple byte stream objects closed by enumeration classes. 5 */6 import java. io. bufferedOutputStream; 7 import java. io. fileInputStream; 8 import java. io. fileOutputStream; 9 import java. io. inputStream; 10 import java. io. sequenceInputStream; 11 import java. util. enumeration; 12 import java. util. vector; 13 14 public class Test1915 {16 public static void main (String [] args) throws Exception17 {18 doSequence (); 19} 20 private static void doSequence () throws Excepti On21 {22 Vector <InputStream> vector = new Vector <InputStream> (); 23 vector. addElement (new FileInputStream ("D:/text1.txt"); 24 vector. addElement (new FileInputStream ("D:/text2.txt"); 25 vector. addElement (new FileInputStream ("D:/text3.txt"); 26 Enumeration <InputStream> e = vector. elements (); 27 SequenceInputStream sis = new SequenceInputStream (e); 28 BufferedOutputStream bos = new BufferedOutputStream (n Ew FileOutputStream ("D:/text4.txt"); 29 int len = 0; 30 for (byte [] buf = new byte [1024*1024]; (len = sis. read (buf ))! =-1;) 31 {32 bos. write (buf, 0, len); 33 bos. flush (); 34} 35 sis. close (); // close (); 36 bos. close (); 37} 38}

 


Main java stream Methods

Java stream operations can be divided into byte streams and byte streams.

1. byte stream

All read Operations inherit from a common superclass java. io. InputStream class.

All write operations inherit from a common superclass java. io. OutputStream class.

Both InputStream and OutputStream are abstract classes.

InputStream has six low-level input streams:

Low-level stream
Stream usage

ByteArrayInputStream
Read data bytes from memory array

FileInputStream
Read data bytes from the local file system

PipedInputStream
Read data bytes from the thread Pipeline

StringBufferInputStream
Reads data bytes from a string.

SequenceInputStream
Reads data bytes from two or more low-level streams, and transfers data from one stream to another when it reaches the end of the stream.

System. in
Read data bytes from the user Console

InputStream also has a subclass: the filter stream java. io. FilterInputStream. The filter stream encapsulates the basic stream for more convenient usage.

The construction method of the FilterInputStream class is FilterInputStream (InputStream). An input stream filter is created on the specified input stream.

Filter input stream
Stream usage

BufferedInputStream
Access data by the buffer to improve efficiency

DataInputStream
Reads basic data types from the input stream, such as int, float, double, or even a line of text.

LineNumberInputStream
Maintain a counter Based on the line terminator of the translation. The counter indicates the row being read.

PushbackInputStream
Data bytes can be pushed back to the first part of the stream.

2. Ghost stream

Note: It was introduced in jdk1.1, And the byte stream above was introduced in jdk1.0. When it is used to process text data, it is better to select the limit stream than the word throttling. However, developers who only use the basic data type can continue to use byte streams.

All read Operations inherit from a common superclass java. io. Reader class.

All write operations inherit from a common superclass java. io. Writer class.

Reader and Writer are also abstract classes.

The common subclass of Reader is as follows:

Low-level Reader
Stream usage

CharArrayReader
Read data from character array

InputStreamReader

FileReader (subclass of InputStreamReader)

Read character sequences from local file systems

StringReader
Read character sequences from strings

PipedReader
Read character sequences from thread Pipelines

InputStreamReader:

InputStreamReader reads data from the input stream and connects the input stream to the reader. For example:

New InputStreamReader (System. in)

Constructor:

InputStreamReader (InputStream)

Use the default character encoding method to create an InputStreamReader.

InputStreamReader (InputStream, String) uses a named character encoding method to create an InputStreamReader. Common filter readers:

Filter Reader

Stream usage

BufferedReader
Buffer Data Access to improve efficiency

LineNumberReader (subclass of BufferedReader)
Maintain a counter that indicates which row is being read... the remaining full text>

What is a java stream? For more information, see

Java stream operations can be divided into byte streams and byte streams.

1. byte stream

All read Operations inherit from a common superclass java. io. InputStream class.

All write operations inherit from a common superclass java. io. OutputStream class.

Both InputStream and OutputStream are abstract classes.

InputStream has six low-level input streams:

Low-level stream
Stream usage

ByteArrayInputStream
Read data bytes from memory array

FileInputStream
Read data bytes from the local file system

PipedInputStream
Read data bytes from the thread Pipeline

StringBufferInputStream
Reads data bytes from a string.

SequenceInputStream
Reads data bytes from two or more low-level streams, and transfers data from one stream to another when it reaches the end of the stream.

System. in
Read data bytes from the user Console

InputStream also has a subclass: the filter stream java. io. FilterInputStream. The filter stream encapsulates the basic stream for more convenient usage.

The construction method of the FilterInputStream class is FilterInputStream (InputStream). An input stream filter is created on the specified input stream.

Filter input stream
Stream usage

BufferedInputStream
Access data by the buffer to improve efficiency

DataInputStream
Reads basic data types from the input stream, such as int, float, double, or even a line of text.

LineNumberInputStream
Maintain a counter Based on the line terminator of the translation. The counter indicates the row being read.

PushbackInputStream
Data bytes can be pushed back to the first part of the stream.

2. Ghost stream

Note: It was introduced in jdk1.1, And the byte stream above was introduced in jdk1.0. When it is used to process text data, it is better to select the limit stream than the word throttling. However, developers who only use the basic data type can continue to use byte streams.

All read Operations inherit from a common superclass java. io. Reader class.

All write operations inherit from a common superclass java. io. Writer class.

Reader and Writer are also abstract classes.

The common subclass of Reader is as follows:

Low-level Reader
Stream usage

CharArrayReader
Read data from character array

InputStreamReader

FileReader (subclass of InputStreamReader)

Read character sequences from local file systems

StringReader
Read character sequences from strings

PipedReader
Read character sequences from thread Pipelines

InputStreamReader:

InputStreamReader reads data from the input stream and connects the input stream to the reader. For example:

New InputStreamReader (System. in)

Constructor:

InputStreamReader (InputStream)

Use the default character encoding method to create an InputStreamReader.

InputStreamReader (InputStream, String) uses a named character encoding method to create an InputStreamReader. Common filter readers:

Filter Reader

Stream usage

BufferedReader
Buffer Data Access to improve efficiency

LineNumberReader (subclass of BufferedReader)
Maintain a counter that indicates which row is being read... the remaining full text>

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.