Author: Jakob Jenkov Translator: Li Jing ([email protected])
This section briefly summarizes the Pushbackinputstream,sequenceinputstream and PrintStream in Java IO. Among them, the most common is printstream,system.out and System.err are printstream types of variables, see Java IO:System.in, System.out, System.err See more information about System.out and System.err.
Pushbackinputstream
Original link
Pushbackinputstream is used to parse data within the InputStream. Sometimes you need to know in advance what byte content is going to be read in order to determine how the data is parsed. Pushbackinputstream allows you to do this, you can re-push the read bytes back into the InputStream to read again through Read (). The code is as follows:
1 |
PushbackInputStream input = new PushbackInputStream( new FileInputStream( "c:\\data\\input.txt" )); |
3 |
int data = input.read(); |
The size of the push-back buffer can be set through the Pushbackinputstream constructor, as follows:
1 |
PushbackInputStream input = new PushbackInputStream( new FileInputStream( "c:\\data\\input.txt" ), 8 ); |
This example sets a buffer of 8 bytes, which means that you can reread up to 8 bytes of data.
Sequenceinputstream
Original link
Sequenceinputstream integrates one or more inputstream to form a logically coherent input stream. When the Sequenceinputstream is read, it is read from the first input stream and then read from the second input stream to push the class. The code is as follows:
1 |
InputStream input1 = new FileInputStream( "c:\\data\\file1.txt" ); |
3 |
InputStream input2 = new FileInputStream( "c:\\data\\file2.txt" ); |
5 |
InputStream combined = new SequenceInputStream(input1, input2); |
With Sequenceinputstream, the 2 InputStream in the example are used as if there were only one inputstream (the translator notes that the Read () method of Sequenceinputstream will be read to the end of the current stream, Closes the stream and points the current stream to the next stream in the logical chain, and finally returns the Read () value of the new current stream).
PrintStream
Original link
PrintStream allows you to write formatted data to the underlying outputstream. For example, write Int,long formatted as text and other raw data types into the output stream, rather than their byte data. The code is as follows:
01 |
PrintStream output = new PrintStream(outputStream); |
05 |
output.print(( int ) 123 ); |
07 |
output.print(( float ) 123.456 ); |
09 |
output.printf(Locale.UK, "Text + data: %1$" , 123 ); |
PrintStream contains 2 powerful functions, namely format () and printf () (the two functions almost do the same thing, but C programmers are more familiar with printf ()).
The Translator notes: one of the printf () functions is implemented as follows:
1 |
public PrintStream printf(String format, Object ... args) { |
3 |
return format(format, args); |
original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java IO: Other byte stream (top)
Java IO: Other byte stream (top)