Java IO: Other byte stream (top)

Source: Internet
Author: User

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 = newPushbackInputStream(newFileInputStream("c:\\data\\input.txt"));
2
3 intdata = input.read();
4
5 input.unread(data);

The size of the push-back buffer can be set through the Pushbackinputstream constructor, as follows:

1 PushbackInputStream input = newPushbackInputStream(newFileInputStream("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 = newFileInputStream("c:\\data\\file1.txt");
2
3 InputStream input2 = newFileInputStream("c:\\data\\file2.txt");
4
5 InputStream combined = newSequenceInputStream(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 = newPrintStream(outputStream);
02
03 output.print(true);
04
05 output.print((int123);
06
07 output.print((float123.456);
08
09 output.printf(Locale.UK, "Text + data: %1$"123);
10
11 output.close();

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 publicPrintStream printf(String format, Object ... args) {
2
3     returnformat(format, args);
4
5 }

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)

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.