"JAVA Other Stream object"

Source: Internet
Author: User

First, the PrintStream class.

The stream is a byte stream.

Extends Filteroutputstream implements Appendable, closeable

Java.lang.Object

|--java.io.outputstream

|--java.io.filteroutputstream

|--java.io.printstream

API Description:

PrintStreamAdded functionality to other output streams to make it easy to print various data value representations. It also provides two other functions. Unlike other output streams, PrintStream they are never thrown, IOException but exceptions only set internal flags that can be checkError tested by methods. In addition, to automatically refresh, you can create one PrintStream ; This means that the method can be called automatically after the byte array is written, either by calling one of the flush println methods, or by writing a newline character or byte ( ‘\n‘ ).

PrintStreamAll characters printed are converted to bytes using the platform's default character encoding. The class should be used in cases where characters need to be written rather than bytes written PrintWriter .

In brief, the characteristics of this flow are:

(1) It does not throw an IO exception.

(2) A printing method is available to print multiple data type values. and can maintain the representation of the data.

(3) If the stream does not specify an encoding type, the default encoding for the platform is applied.

1. Construction method.

Parameters that can be accepted by the construction method include:

(1) String path

(2) File object

(3) Byte output stream

2. Common methods.

 void close()
Closes the stream.
 void flush()
Refreshes the buffer of the stream.

Printsteam class to output information, it must be refreshed. If you want to refresh automatically, you should use a construction method that has the automatic refresh feature. If you want to write to a file and refresh automatically, you need to construct the byte output stream before passing it as a parameter to the construction-drop method.

 void print(基本数据类型 x)
The print base data type does not wrap, using the platform's default character encoding.
 void println(节本数据类型 x)
Prints the base data type to the specified object, uses the default character encoding of the platform and adds the line delimiter on the platform at the end to implement the line wrapping function.
 void write(byte[] buf, int off, int len)
lenwrites a byte array of bytes from the specified initial offset to off this stream.
 void write(int b)
Writes the specified bytes to this stream.

What is the difference between the print and write methods?

The Print method converts the printed base data type first into a string and then

Print; The Write method takes the printed integer data down by eight bits to one byte before printing.

Print (97); the result is 97; and write (97); The result becomes a.

Second, the PrintWriter class.

The stream is a character stream.

PrintWriterextends Writer

Java.lang.Object

|--java.io.writer

|--java.io.printwriter

The methods in this class are almost identical to those in PrintStream, not to repeat them.

However, it should be noted that the two have a common feature, that is, if the data is written directly to the file, without the automatic refresh function, if you want to write to the file and also to automatically refresh, you need to first use the output stream to encapsulate the file.

Third, Sequence Flow: Sequenceinputstream class.

Sequenceinputstreamextends InputStream

Java.lang.Object

|--java.io.inputstream

|--java.io.sequenceinputstream

The stream is capable of merging multiple input streams into one stream.

1. Construction method.

SequenceInputStream(Enumeration<? extends InputStream> e)
By remembering the parameters to initialize the newly created SequenceInputStream , the parameter must be a type parameter that generates the run-time type as an InputStream object Enumeration .
SequenceInputStream(InputStream s1, InputStream s2)
By remembering these two parameters to initialize the newly created SequenceInputStream (the two parameters will be read sequentially, read first s1 , and then read s2 ) to provide SequenceInputStream the bytes read from this.

Of these two construction methods, the first method is more commonly used, because the collections can be used to store each stream so that it is handled so well.

2. Common methods.

Slightly.

3. Example.

"JAVA Other Stream object"

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.