Crazy Java Learning notes (SI)----------detailed java IO (full)

Source: Internet
Author: User
Tags object serialization

The big family of the stream

The concept of streaming (stream):

Originated from the concept of piping (pipe) in Unix. In Unix, a pipeline is an uninterrupted stream of bytes used to communicate between programs or processes, or to read and write peripherals, external files, and so on.


a stream, which must have both the source and destination , can be some area of computer memory, a disk file, or even a URL on the Internet.


The direction of the flow is important, depending on the direction of the flow, the flow can be divided into two categories: the input stream and the output stream.

The user can read the information from the input stream, but cannot write it. Instead, the output stream can only be written to the input stream, not read it.  

In fact, the source and destination of a stream can simply be seen as a producer and consumer of bytes, and for an input stream, you don't have to care what its source is, simply read the data from the stream, and on the output stream, you don't know its destination, just simply write the data to the stream.

image metaphor -water flow, file ====== program:a pipe is connected between the file and the program, and the flow of water is formed, and naturally there is a direction: it can flow in and out. Easy to understand, so define the flow: a stream is a pipe in which there is water, and this pipe connects files and programs.

The classes in the Java.io package correspond to two types of flow

A class of streams is read or written directly from a specified location (such as a disk file or memory region), which is called a node stream, and other streams are called filters (filters).

The filter input stream is often used as its input source for other input streams, filtered or processed and then provided to the user in the form of a new input stream, and the filter output stream is similar in principle.

Streams in Java can be categorized from different angles.

by number depending on the direction of the flow can be divided into: input stream and output stream.

According to the different units of processing data can be divided into: byte stream and character stream.

According to the implementation of different functions can be divided into: node flow and processing flow.

Output stream:

Input stream:

So both the input and the output are from the point of view of the program.

BYTE stream: A read-in or read-out is a 8 -bit binary.

Character Stream: A read-in or read- out is a binary binary.

The principle of a byte stream and a character stream is the same, except that the units are handled differently. The suffix is stream is a byte stream, and the prefix is Reader, andWriter is a stream of characters.

Node stream: Directly connected to the data source, read in or read out.

Direct use of node flow, read and write inconvenient, in order to read and write files faster, only the processing stream.

Processing flow: With the node flow a piece of use, on the basis of the node flow, and then a layer, the socket on the node flow is the processing flow.

The Jdk provides a stream that inherits four classes:inputstream ( byte input stream ),outputstream(byte output stream),Reader (character input stream),Writer(character output stream).

The following are the common streams in io in java .

If you are still very confused, then please read the following text, you will be enlightened!

One, what is the flow?

Flow is the abstract concept of a byte sequence, the data source that can be continuously read data and the receiving end that can be continuously written to the data is a stream, the flow mechanism is an important mechanism in Java and C + +, through which we can freely control the flow of data such as file, memory, IO device and so on. The IO stream is used to process the data on the device, such as: Hard disk, memory, keyboard input, etc. Depending on the processing type, the IO stream can be divided into byte stream and character stream, which can be divided into input stream and output stream depending on the flow direction.

Two, the difference between the byte stream and the character stream:

Character Stream, because of the different file encoding, there is a character stream object efficient operation, it is based on the byte stream reading bytes when the specified code table is checked. It differs from the byte stream by two points: 1. When the data is read, the byte stream reads to a bytes to return a byte, the character flow is read to one or more bytes using the byte throttle (a Chinese corresponds to the number of bytes is two, in the UTF-8 Code table is 3 bytes), the first to check the specified encoding table, and then return the detected characters; 2. Byte stream can handle all Types of data, such as JPG, AVI, MP3, WAV, and so on, and character streams can only handle the characters data. So you can consider using a byte stream or a character stream depending on which file is being processed, and if it is plain text data, you can prioritize character streams, otherwise use a byte stream.

Each character stream and byte stream are split!

BYTE input stream:

BYTE output stream:

Character input stream:

Character output stream:

Simply introduce it:

operation of the file :fileinputstream(byte input stream),fileoutputstream(byte output stream),filereader(character input stream), FileWriter(character output stream)

operation of the pipeline :pipedinputstream(byte input stream), Pipedoutstream(byte output stream),Pipedreader (character input stream),pipedwriter(character output stream)

An instance of PipedInputStream is used in conjunction with an instance of PipedOutputStream to complete the read-write operation of the pipeline. Mainly used for threading operations.

Byte / character array:Bytearrayinputstream,bytearrayoutputstream,chararrayreader, Chararraywriter is a byte or character array that is opened in memory.

Buffered Buffer stream::bufferedinputstream,bufferedoutputstream,BufferedReader, BufferedWriter, which is a processing stream with buffers, the main purpose of the buffer is to avoid dealing with the hard disk every time and improve the efficiency of data access.

Conversion Flow :inputstreamreader/outputstreamwriter, converts bytes into characters.

Data Flow :datainputstream,dataoutputstream.

Because if we output a long type of 8 bytes or a float of 4 bytes, what do we do? You can output one byte at a time, or you can convert it to a string output, but it's good to have a direct output, so this data flow solves the difficulty of our output data type. Data flow can directly output float type or long type, which improves the efficiency of data reading and writing.

Print flow :printstream,printwriter, is typically printed to the console, where you can control the printing.

Object Flow :objectinputstream,objectoutputstream, outputs the encapsulated object directly, rather than converting it into a string and then outputting it.

serialized stream :sequenceinputstream.

Object Serialization : Converts the object directly to binary and writes to the media.

the use of object flow requires the implementation of the Serializable interface, or an error occurs .

If the member variable is modified with the transient keyword, the member variable is not written, and if the member variable of the reference type is null, the member variable of the value type is 0.

Java byte stream

    • FileInputStream and FileOutputStream
      These two classes belong to the node stream, the source side of the first class and the destination end of the second class are disk files, and their construction methods allow the corresponding stream to be constructed through the file's path name. Such as:
      FileInputStream infile = new FileInputStream ("Myfile.dat");
      FileOutputStream outfile = new FileOutputStream ("Results.dat");

note that the FileInputStream is constructed, the corresponding file must be present and readable, and when the FileOutputStream is constructed, such as the output file already exists, it must be overwritten.

    • Bufferedinputstream and Bufferedoutputstream
      They are filter flows, and their effect is to improve the efficiency of the input and output.
    • DataInputStream and DataOutputStream
      The objects created by these two classes are called data input streams and data output streams, respectively. This is a useful two stream that allows programs to read and write Java data in a machine-independent style. Therefore, it is more suitable for data transmission on the network. These two streams are also filter flows, often with other flows such as InputStream or outputstream as their inputs or outputs.
character stream of Java
Character streams are primarily used to handle characters. Java uses 16-bit Unicode to represent strings and characters, and the corresponding character streams are called readers and writers by input and output respectively.
    • InputStreamReader and OutputStreamWriter
      When constructing the corresponding streams for these two classes, they are automatically converted to convert the platform's default encoding set of encoded bytes to Unicode characters. For the English environment, its default encoding set is generally iso8859-1.
    • BufferedReader and BufferedWriter
      These two classes correspond to a stream that uses buffering, which can greatly improve the efficiency of the input and output. These two are also filter flows, which are often used to process inputstreamreader and OutputStreamWriter.

This paper draws on

http://blog.csdn.net/yuebinghaoyuan/article/details/7388059

Http://www.cnblogs.com/pepcod/archive/2013/01/20/2913435.html

Crazy Java Learning notes (SI)----------detailed java IO (full)

Related Article

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.