Java Fundamentals Review IO

Source: Internet
Author: User

Java io is one of the important parts of Java application, involving more and more content, easy to confuse, a period of time, may be forgotten, to often recall memory:

(Image from the network)

The Java stream is processed into a character stream and a byte stream.

A character stream processes a cell that is 2-byte Unicode characters, manipulating characters, character arrays, or strings, and a byte-stream processing unit of 1 bytes, manipulating bytes and byte arrays.
In Java, characters are stored in Unicode encoding, and the character stream processing class is responsible for converting externally encoded character streams and the stream of Unicode characters within Java.
Class InputStreamReader and OutputStreamWriter handle conversion of character streams and byte streams. Character stream (one buffer can be processed at a time) one operation is more efficient than a byte stream (one bytes at a time).

Bytearrayinputstream--Uses a buffer in memory as a inputstream.
StringBufferInputStream--Put a string object as a InputStream.
FileInputStream--A file is used as a inputstream to read the file.
PipedInputStream-implements the concept of pipe, which is used in the main thread. Pipeline inflow refers to the receiving end of a communication pipeline.
Sequenceinputstream-merge multiple inputstream into one inputstream. The sequence input stream class allows applications to merge several input streams continuously and make them appear as a single input stream. Each input stream is read sequentially until the end of the stream is reached. The sequence input stream class then closes the stream and automatically switches to the next input stream.


The InputStreamReader class is a bridge that flows from byte to character stream.

general usage Principles for Java IO:

first, according to the data source (whereabouts) Classification:

1, is the file: FileInputStream, FileOutputStream, (Byte stream) FileReader, FileWriter (character)

2, is byte[]: Bytearrayinputstream, Bytearrayoutputstream (Byte stream)

3, is char[]: CharArrayReader, Chararraywriter (character stream)

4, is String:stringbufferinputstream, Stringbufferouputstream (Byte stream) StringReader, StringWriter (character stream)

5, network Data flow: InputStream, OutputStream, (Byte stream) Reader, Writer (character stream)

second, according to whether to format the output points:

1. To format the output: PrintStream, PrintWriter

third, press whether to buffer points:

1, to buffer: Bufferedinputstream, Bufferedoutputstream, (Byte stream) BufferedReader, BufferedWriter (character stream)

iv. by Data format:

1. binary format (as long as it cannot be determined to be plain text): InputStream, OutputStream and all subclasses with Stream end

2, plain text format (including pure English and Chinese characters or other encoding); reader, writer and all the subclasses of reader, writer

Five, according to the input and output points:

1. Input: Reader, subclass of InputStream type

2. Output: Writer, subclass of OutputStream type

Vi. Special Needs:

1. Conversion class from Stream to Reader,writer: InputStreamReader, OutputStreamWriter

2, object input and output: ObjectInputStream, ObjectOutputStream

3. Interprocess communication: Pipeinputstream, Pipeoutputstream, Pipereader, Pipewriter

4. Merge input: Sequenceinputstream

5, more special needs: Pushbackinputstream, Pushbackreader, Linenumberinputstream, LineNumberReader


The general guidelines for deciding which class to use and its construction process are as follows (regardless of special needs):

First, consider what the most primitive data format is: Principle Four

The second is the input or output: Principle five

Third, do you need to convert the flow: Principle Six 1th

What is the source of data (whereabouts): Principle One

Five, whether to buffer: principle Three (Special note: It is important to pay attention to the readLine () whether there is a definition, what is more than read, write a more special input or output method)

VI, do you want to format the output: Principle two

Java Fundamentals Review IO

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.