Java IO Stream

Source: Internet
Author: User

IO streams are mainly used for data manipulation on devices such as hard disks, memory, keyboards, etc., depending on the data type of the data being processed can be divided into: byte stream (abstract base class is InputStream and OutputStream) and character stream (abstract base class for reader and writer). Depending on the flow direction, it can be divided into: input stream and output stream. The main structure diagram of IO is as follows:

What is the main difference between a character stream and a byte stream?

1. When reading a byte stream, a byte is read, and a character stream reads one or more bytes using a byte flow (there are two bytes in Chinese, and 3 bytes in the UTF-8 encoding table), the specified encoding table is first checked, and the detected characters are returned

2. Byte stream can handle all kinds of data, such as: Picture, Mp3,avi video file. The character stream can only handle characters data, as long as it is processing plain text data, priority should be given to the use of character streams, in addition to the byte stream

The IO stream can be divided into two main types, node flow and processing flow.

One, node flow type

This type can read data from or to a specific location or node. The main types are as follows

Type Character Stream BYTE stream
File (Files)

FileReader

FileWriter

FileInputStream

FileOutputStream

Memory Array

CharArrayReader

Chararraywriter

Bytearrayinputstream

Bytearrayoutputstream

Memory String

StringReader

StringWriter

Pipe (piping)

Pipedreader

PipedWriter

PipedInputStream

PipedOutputStream

Ii. Processing Flow Type

The type is to encapsulate an already existing stream, and to read and write the data by encapsulating the stream, and the method of constructing the flow is always to take one other stream object as the parameter, and one stream object passes through the other stream's multiple wrappers, called the stream link. The main can be divided into the following types:

1. Buffer Stream (Bufferedinputstream/bufferedoutputstream/bufferedwriter/bufferedreader)

Write Buffer object: bufferedwriter BUFW = new BufferedWriter (New FileWriter ("Buf.txt"));

Read Buffer object: BufferedReader bufr = new BufferedReader (New FileReader ("Buf.txt"));

There is a unique method for this type of flow: ReadLine (), reading one line at a time, returning the character data before the row tag as a string, returning NULL when read to the end, or the Read method of the stream object associated with the buffer, except that every time it is read to a character, The temporary storage is not performed first, and the data stored in the staging container is returned at once when the carriage return tag is read

2. Conversion Stream (Inputstreamreader/outputstreamwriter)

The type is a bridge between the byte stream and the character stream, which allows the encoding conversion of the specified encoding to be read to the bytes data.

The main constructors are: InputStreamReader (InputStream);

InputStreamReader (inputstream,string charSet);

OutputStreamWriter (OutputStream);

OutputStreamWriter (outputstream,string charSet);

3. Data stream (Datainputstream/dataoutputstream)

This data stream facilitates direct storage and reading of some basic types of data without further conversion, usually by datastream, as long as the data of the basic data type is manipulated.

Construction method: DataInputStream (InputStream)

DataOutputStream (OutputStream)

Examples of methods:

int readInt ();

Writeint (int);

Short Readshort ();

Writeshort (short);

String readUTF ();

writeUTF (String)

Note: In the use of data stream read/save data, need to have a certain order, that is, a type of data written first must be read first, obey the principle of FIFO

4. Print Flow

PrintStream is a byte print stream, System.out corresponds to a type of printstream, and its constructor can accept values of three data types: 1. String path. 2.File object. 3.OutputStream

PrintWriter is a character print stream

5. Object Flow

This type of flow can access the class as a whole, the main methods are:

Object ReadObject () This method throws an exception: Classnotfountexception

void WriteObject (Object): The object being written must implement an interface: Serializable, otherwise it will be thrown: notserializableexception

Java IO Stream

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.