Simple use of input/output streams

Source: Internet
Author: User

The output stream output stream has two sets of class structure: First, a byte-stream-based class with OutputStream as the base class, and a character stream-based class with write as the base class.
1, the Outputstreamoutputstream class includes the main function of the class is to determine where to output, such as to send to a file (derived class FileOutputStream), or to the pipeline (derived class PipedOutputStream) and so on.

2, decoration class decoration class concept, is on the original basis, to add a function can be added by adding a decoration to achieve. Compared with the inheritance mechanism, the decoration class is dynamic and can add or remove additional functions at run time with greater flexibility. Filteroutputstream is a base class for decorating classes that connects attributes or useful interfaces to output streams for other OutputStream classes (such as dateoutputstream\ Bufferoutputstream) provides useful functionality.

Dateoutputstream
Used in conjunction with Dateinputstream to write the original data type to a data stream in a "platform independent" form. such as Readboolean (), readUTF (), Readchar,readbyte (), readdouble (), etc. All read methods share a positional indicator, that is, after the previous Read method executes, the other read methods are read back from the location read from the previous Read method. The Readchar () after 1 readbyte () is read starting from the 2nd byte.

Bufferedoutputstream
The decoration class is used to put the data into the buffer first, and to avoid the physical writing of the data that is sent out very little at a time. by calling Flush (), the buffer is refreshed to complete the physical write. Note that the flush () method clears the buffer and forces the bytes that were previously buffered to write to the stream to be written immediately.

When using sockets for communication
try {            dis=new datainputstream (New Bufferedinputstream (Socket.getinputstream ()));            Dos=new DataOutputStream (New Bufferedoutputstream (Socket.getoutputstream ()));        } catch (IOException e) {            e.printstacktrace ();        }

3. The Write class write class is primarily used in Unicode-oriented, character-based I/O operations. The Write class is an abstract class, not a real data stream, but it provides the user with a way to write a binary as a stream of characters.
Subclass of Reader class:
Bufferdwrite: Writes text to the output stream, providing buffering of the characters written
Pipedwrite: Writing characters to Pipedreader
Printwrite: Write a formatted character (format the binary number as a character and write it to the data stream)
Stringwrite: Writing characters to a string
Filterwrite: Write Filtered Stream
Outputstreamwrite: Writing Stream
Chararraywrite: Writing characters to a character array

Second, the input stream is the same as the output stream, the input stream is also divided into Inpututstream and Reader, the former is based on byte stream.
1. Inputstreaminputstream reads the data from the stream by calling the Read method. By calling the Close method to close InputStream, closing the input stream and then attempting to invoke it will result in an exception.

2, reader class reader class for the character stream read processing.
The subclass of the reader class corresponds to the reader class. Where inputstreamreader can convert binary flow into a character stream.
For example, reading data from the network
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
where the getInputStream () method returns the socket class byte stream, which is converted to a character stream through InputStreamReader and encapsulated by BufferedReader, You can then use the method provided by BufferedReader ReadLine to read the string one line at a time.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple use of input/output streams

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.