Java Io stream learning Summary

Source: Internet
Author: User

Classes or interfaces related to Java stream operations:

Java stream class diagram structure:

 

Concept and function of stream

A stream is a set of sequential bytes with a starting point and an ending point. It is a general term or abstraction for data transmission. That is to say, data transmission between two devices is called a stream,The essence of a stream is data transmission. The stream is abstracted into various types based on the data transmission characteristics to facilitate more intuitive data operations.

 

Io stream classification

Data types can be divided into two types: dense stream and word throttling.

Data flows are divided into input streams and output streams.

Traffic and word throttling

The origin of the character stream: because of the different data encoding, there is a stream object for efficient character operations. In essence, when reading data based on byte streams, the specified code table is checked. Differences between byte stream and byte stream:

  • Different read/write units: byte streams are measured in 8 bits and byte streams are measured in characters. Based on the ing characters in the code table, multiple bytes may be read at a time.
  • Different Processing objects: byte streams can process all types of data (such as slice and AVI), while character streams can only process data of character types.

Conclusion: as long as the plain text data is processed, the use of the plain stream is preferred. In addition, byte streams are used.

 

Input stream and output stream

Only read operations can be performed on the input stream, and only write operations can be performed on the output stream. Different streams must be used in the program based on different features of the data to be transmitted.

Java Io Stream Object

 

1. input byte stream inputstream

The inheritance diagram of input byte streams in Io is visible, as shown in the following figure:

  1. Inputstream is the parent class of all input byte streams. It is an abstract class.
  2. Bytearrayinputstream, stringbufferinputstream, and fileinputstream are three basic media streams that read data from byte arrays, stringbuffer, and local files respectively. Pipedinputstream reads data from pipelines shared with other threads. piped-related knowledge will be introduced separately later.
  3. Objectinputstream and all the sub-classes of filterinputstream are decorative streams (the main character of the decoration mode ).
2. Output byte stream outputstream

The inheritance diagram of the output byte stream in Io is visible, as shown in the following figure:

  1. Outputstream is the parent class of all output byte streams. It is an abstract class.
  2. Bytearrayoutputstream and fileoutputstream are two basic media streams, which write data to byte arrays and local files respectively. Pipedoutputstream writes data to the pipe shared with other threads,
  3. Objectoutputstream and all the subclasses of filteroutputstream are decorative streams.
3. Correspondence between input and output of byte streams

In the figure, the blue part is the main corresponding part, and the red part is the non-corresponding part.The purple dotted line indicates that these streams are usually used together.. From the figure above, we can see that the byte stream in Java Io is extremely symmetric. "Existence is reasonable." Let's look at several classes that are not symmetric in these byte streams!

  1. Linenumberinputstream obtains the corresponding row number when it reads data from the stream. When and where the branch is determined by the change class, there is not such a line number in the original. There is no corresponding part in the output part. We can create a linenumberoutputstream by ourselves. There will be a baseline line number when we first write data, and a line number will be added to the next line each time we encounter a line change, it looks okay, too. It seems that it is not streaming.
  2. Pushbackinputstream is used to view the last byte and put it into the buffer zone if it is not satisfied. It is mainly used in Compiler syntax and lexical analysis. The bufferedoutputstream of the output is almost similar.
  3. The stringbufferinputstream has been deprecated and should not appear in the inputstream part, mainly because the string should belong to the shard stream range. It has been deprecated. Of course, it is not necessary for the output part! It is also allowed to exist only to keep the version backward compatible.
  4. Sequenceinputstream can be considered as a tool class. It reads two or more input streams as one input stream in sequence. It can be removed from the IO package completely without affecting the structure of the IO package, but making it "pure"-pure decorator mode.
  5. Printstream can also be considered as an auxiliary tool. Data can be written to other output streams, or fileinputstream. The internal implementation is still buffered. Essentially, it is a tool for the comprehensive use of other streams. I/O packages can be kicked out! System. Out and system. out are printstream instances!
4. character input stream Reader

From the inheritance graph above, we can see that:

  1. Reader is the parent class of all input streams. It is an abstract class.
  2. Charreader and stringreader are two basic media streams. They read data from Char arrays and strings respectively. Pipedreader reads data from pipelines shared with other threads.
  3. Bufferedreader is obviously a decoration device, and its sub-classes are used to decorate other reader objects.
  4. Filterreader is the parent class of all custom decorative streams. Its subclass pushbackreader is used to decorate the reader object and adds a row number.
  5. Inputstreamreader is a bridge between byte streams and primary streams. It converts byte streams into primary streams. Filereader can be said to be a commonly used tool class to achieve this function. In its source code, the fileinputstream is obviously used as a reader method. We can get some tips from this class. The usage and usage of each class in reader are basically the same as that in inputstream. There will be a ing between reader and inputstream.
5. character output stream writer

From the graph above, we can see that:

  1. Writer is the parent class of all output streams. It is an abstract class.
  2. Chararraywriter and stringwriter are two basic media streams that write data to Char arrays and strings respectively. Pipedwriter writes data to pipelines shared with other threads,
  3. Bufferedwriter is a decorator that provides the buffer function for writer.
  4. Printwriter and printstream are extremely similar in functionality and usage.
  5. Outputstreamwriter is a bridge between outputstream and writer. Its subclass filewriter is actually a specific class that implements this function (you can study sourcecode ). Functions and usage are very similar to those of outputstream, and their corresponding graphs will be shown later.
6. Correspondence between input and output of the livestream

7. byte stream and byte stream conversion

Conversion stream features:

  1. It is a bridge between the traffic flow and the word throttling.
  2. You can convert the byte data to a character through specified encoding.
  3. Character data can be converted to bytes by specified encoding.

When to use a conversion stream?

  1. When there is a conversion between byte and character;
  2. When the stream operation data needs to be encoded or decoded.

Specific Object embodiment:

  1. Inputstreamreader:Byte to character Bridge
  2. Outputstreamwriter:Character to byte Bridge

These two stream objects are members of the character system. They have a conversion function, and they are character streams. Therefore, byte stream objects need to be input during construction.

8. File class

A file class is an object that encapsulates files and folders in a file system. You can use the object idea to operate files and folders. File class stores various metadata information about a file or directory, including the file name, file length, last modification time, whether the file is readable, and obtaining the path name of the current file, determine whether a specified file exists, obtain the file list in the current directory, and create or delete files and directories.

9. randomaccessfile class

This object is not a member of the stream system. It encapsulates the byte stream and a buffer (character array) to operate data in the character array through internal pointers. Features:

  1. This object can only operate on files. Therefore, the constructor receives two types of parameters: A. String file path and B. File object.
  2. This object can both read and write files. You can specify the operation mode (R, RW) when instantiating objects)

Note: When the object is instantiated, if the file to be operated does not exist, it will be automatically created. If the file exists and the write data is not specified, it will be written from the beginning, that is, overwrite the original content.It can be used to download data in multiple threads or write data to files simultaneously.

This article is transferred from:
Java Io stream learning Summary

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.