Home  >  Article  >  Java  >  Java programming IO flow information

Java programming IO flow information

高洛峰
高洛峰Original
2017-02-27 15:40:521203browse

This article mainly introduces the relevant information that is compiled in detail on the IO flow information of Java programming. Friends who need it can refer to

java IO detailed explanation:

Java Classes or interfaces related to stream operations:

Java 编程之IO流资料

Java stream class diagram structure:

Java 编程之IO流资料

The concept of stream And function

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

Classification of IO streams

  • According to the different types of data processed, it is divided into: character stream and byte stream

  • According to different data flow directions, it is divided into: input stream and output stream

Character stream and byte stream

The origin of the character stream: Because of the different data encodings, there are stream objects that perform efficient operations on characters. The essence is actually to check the specified code table when reading based on the byte stream. The difference between byte stream and character stream:

  • The reading and writing units are different: the byte stream is in bytes (8bit) as the unit, the character stream is in characters as the unit, and the characters are mapped according to the code table , multiple bytes may be read at a time.

  • The processing objects are different: byte streams can process all types of data (such as pictures, avi, etc.), while character streams can only process character type data.

Conclusion: As long as you are dealing with plain text data, give priority to using character streams. Otherwise, byte streams are used.

Input stream and output stream

Only the input stream can be read, and the output stream can only be written. The program needs to use different streams according to the different characteristics of the data to be transmitted. .

Java IO stream object

1. Input byte stream The inheritance diagram of the input byte stream in InputStreamIO can be seen in the figure above, as can be seen:

  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. They read data from Byte arrays, StringBuffer, and local files respectively. PipedInputStream reads data from a pipe shared with other threads. Knowledge related to Piped will be introduced separately later.

  3. ObjectInputStream and all subclasses of FilterInputStream are decoration streams (the protagonists of the decorator pattern). 2. Output byte stream OutputStream

The inheritance diagram of the output byte stream in IO can be seen in the figure above. It can be seen that:

OutputStream is all output byte streams The parent class of , which is an abstract class. ByteArrayOutputStream and FileOutputStream are two basic media streams. They write data to Byte arrays and local files respectively. PipedOutputStream writes data to a pipe shared with other threads. ObjectOutputStream and all subclasses of FilterOutputStream are decoration streams. 3. The correspondence between the input and output of the byte stream

Java 编程之IO流资料

The blue part in the figure is the main corresponding part, and the red part is the non-corresponding part. The purple dotted line indicates that these streams are generally used together. As can be seen from the above figure, the byte stream in Java IO is extremely symmetrical. "Existence and Reasonability" Let's take a look at some of the less symmetrical classes in these byte streams!

1.LineNumberInputStream mainly completes reading data from the stream, and will get the corresponding line number. As for when and where to branch, it is determined actively by the class change, not in the original version. Line number. There is no corresponding part in the output part. We can create a LineNumberOutputStream ourselves. There will be a baseline line number when initially written. In the future, a line number will be added to the next line every time a line break is encountered. It seems to be OK. It seems even less popular.

2. The function of PushbackInputStream is to check the last byte and put it into the buffer if it is not satisfied. Mainly used in the syntax and lexical analysis parts of the compiler. The BufferedOutputStream in the output part almost implements similar functions.

3.StringBufferInputStream has been Deprecated and should not appear in the InputStream part, mainly because String should belong to the scope of the character stream. It has been abandoned, of course there is no need for it in the output part! It is also allowed to exist just to maintain backward compatibility of versions.

4.SequenceInputStream can be considered as a tool class that reads two or more input streams as one input stream in sequence. It can be completely removed from the IO package, and it does not affect the structure of the IO package at all, but makes it more "pure" - pure Decorator mode.

5.PrintStream can also be considered as an auxiliary tool. It can mainly write data to other output streams or FileInputStream, and its internal implementation is still buffered. Essentially, it is just a tool for the comprehensive application of other streams. You can also kick out IO packages! System.out and System.out are instances of PrintStream!

4. Character input stream Reader

As can be seen in the inheritance diagram above:

1.Reader is all input character streams The parent class of , which is an abstract class.

2.CharReader and StringReader are two basic media streams. They read data from Char array and String respectively. PipedReader reads data from a pipe shared with other threads.

3.BufferedReader is obviously a decorator, and it and its subclasses are responsible for decorating other Reader objects.

4.FilterReader is the parent class of all custom specific decoration streams. Its subclass PushbackReader decorates the Reader object and adds a line number.

5.InputStreamReader is a bridge that connects byte streams and character streams. It converts byte streams into character streams. FileReader can be said to be a commonly used tool class to achieve this function. In its source code, the method of converting FileInputStream into Reader is obviously used. We can get certain tricks from this class. The purposes and usage methods of each class in Reader are basically the same as those in InputStream. There will be a corresponding relationship between Reader and InputStream later. 5. Character output stream Writer

As can be seen in the above relationship diagram:

1.Writer is the parent class of all output character streams, and it is an abstract class.

2.CharArrayWriter and StringWriter are two basic media streams. They write data to Char array and String respectively. PipedWriter writes data to a pipe shared with other threads.

3.BufferedWriter is a decorator that provides buffering function for Writer.

4.PrintWriter and PrintStream are very similar, and their functions and uses are also very similar.

5.OutputStreamWriter is the bridge for conversion from OutputStream to Writer. Its subclass FileWriter is actually a specific class that implements this function (you can study SourceCode for details). The functions and usage are very similar to OutputStream, and their corresponding diagrams will be shown later. 6. Correspondence between input and output of character stream

Java 编程之IO流资料

7. Character stream and byte stream conversion

Characteristics of conversion stream :

  1. It is the bridge between character stream and byte stream

  2. It can convert the read byte data through specified encoding Into characters

  3. The read character data can be converted into bytes through the specified encoding

When to use the conversion stream?

  1. When there is conversion action between bytes and characters;

  2. When the data of stream operation needs to be encoded or decoded.

Specific object representation:

  1. InputStreamReader:Bridge from byte to character

  2. OutputStreamWriter:Character to byte bridge

#These two stream objects are members of the character system. They have conversion functions and themselves It is a character stream, so you need to pass in a byte stream object during construction.

8.File class

The File class is an object that encapsulates files and folders in the file system. Files and folders can be manipulated through the idea of ​​objects. The File class saves various metadata information of files or directories, including file name, file length, last modification time, whether it is readable, obtaining the path name of the current file, determining whether the specified file exists, obtaining the file list in the current directory, and creating , delete files and directories, etc.

9.RandomAccessFile class

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

This object can only operate files, so the constructor receives two types of parameters: a. String file path; b. File object. This object can both read and write files. When instantiating the object, you can specify the operation mode (r, rw)

Note: The object is instantiated , if the file to be operated does not exist, it will be automatically created; if the file exists and the location of the write data is not specified, it will be written from the beginning, that is, the original content will be overwritten. Can be used for multi-thread downloading or multiple threads writing data to files at the same time.

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more articles related to Java programming IO flow information, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn