In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand JAVA IO flow". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand JAVA IO flow".
I. the concept and function of flow
A stream is a sequential set of bytes with a starting point and an end point, which is a general term or abstraction for data transmission. That is, the transmission of data between the two devices is called stream, and the essence of flow is data transmission. According to the characteristics of data transmission, the stream is abstracted into various types, which is convenient for more intuitive data operation.
II. Classification of streams
1. Character stream and byte stream (dealing with data typing)
The origin of character streams: because of different data encodings, there are stream objects that operate efficiently on characters. The essence is to check the specified code table when reading based on the byte stream.
two。 Input stream and output stream (data flow division)
The input stream can only be read and the output stream can only be written. Different streams need to be used in the program according to the different characteristics of the data to be transferred.
Third, the difference between character stream and byte stream
1. Read and write units are different: the byte stream is in bytes (8bit), the character stream is in characters, and characters are mapped according to the code table, and multiple bytes may be read at a time.
two。 Processing objects are different: byte streams can handle all types of data (such as pictures, avi, etc.), while character streams can only handle data of character types.
3. The byte stream itself does not use the buffer during the operation, but is directly operated by the file itself, while the character stream uses the buffer after the operation, and manipulates the file through the buffer. We will verify this below.
Summary: byte stream is preferred. First of all, because all the files on the hard disk are transferred or saved in the form of bytes, including pictures and other contents. But characters are formed only in memory, so byte streams are widely used in development.
4. The structure of Java flow class diagram
5. JAVA IO stream object
1. Input byte stream InputStream
Definition and structure description:
InputStream is the parent class of all input byte streams, it is an abstract class.
ByteArrayInputStream, StringBufferInputStream, and FileInputStream are three basic media streams that read data from the Byte array, StringBuffer, and local files, respectively. PipedInputStream reads data from pipes that are shared with other threads, and knowledge related to Piped is described separately later.
ObjectInputStream and all subclasses of FilterInputStream are decoration streams (the protagonist of the decorator pattern). This means that the FileInputStream class can create an object, FileInputStream (String name), with a String pathname. DataInputStream must decorate a class to return an object, DataInputStream (InputStream in).
two。 Output byte stream OutputStream
Definition and structure description:
The inheritance diagram of the output byte stream in IO can be seen in the figure above. You can see:
OutputStream is the parent class of all output byte streams, which is an abstract class.
ByteArrayOutputStream and FileOutputStream are two basic media streams that write data to the Byte array and the local file, respectively. PipedOutputStream writes data to pipes that are shared with other threads
ObjectOutputStream and all subclasses of FilterOutputStream are decoration streams.
3. Character input stream Reader
Definition and description:
As you can see in the inheritance diagram above:
Reader is the parent class of all input character streams, it is an abstract class.
CharReader and StringReader are two basic media streams, which read data from Char array and String respectively. PipedReader reads data from pipes that are shared with other threads.
BufferedReader is clearly a decorator, and it and its subclasses are responsible for decorating other Reader objects.
FilterReader is the parent class of all custom concrete decoration streams, and its subclass PushbackReader decorates the Reader object with a line number.
InputStreamReader is a bridge between a byte stream and a character stream, transforming a byte stream into a character stream. FileReader can be said to be a commonly used tool class to achieve this function, and its source code clearly uses the method of turning FileInputStream into Reader. We can get some skills from this category. The purpose and usage of each class in Reader is basically the same as that of classes in InputStream. There will be a correspondence between Reader and InputStream.
4. Character output stream Writer
Definition and description:
As can be seen in the diagram above:
Writer is the parent class of all output character streams, and it is an abstract class.
CharArrayWriter and StringWriter are two basic media streams, which write data to Char array and String respectively.
PipedWriter writes data to pipes that are shared with other threads
BufferedWriter is a decorator that provides buffering for Writer.
PrintWriter and PrintStream are very similar, and their functions and usage are very similar.
OutputStreamWriter is the bridge from OutputStream to Writer, and its subclass FileWriter is actually a concrete class that implements this function (specifically, you can study-SourceCode). The function and use are very similar to OutputStream, and their corresponding diagrams will be shown later.
VI. Analysis of special input streams
1. LineNumberInputStream
The main completion of reading data from the stream, will get the corresponding line number, as for when the branch, where the branch is actively determined by the change, there is not such a line number in the original. There is no corresponding part in the output section, so we can create a LineNumberOutputStream ourselves, with a base line number at the beginning of the write, and a line number on the next line every time we encounter a line break later.
2. PushbackInputStream
Its function is to view the last byte and put it in the buffer if you are not satisfied. Mainly used in the syntax and lexical analysis of the compiler. The BufferedOutputStream in the output part almost achieves similar functions.
3. StringBufferInputStream
Has been Deprecated, itself should not appear in the InputStream section, mainly because String should fall within the scope of the character stream. It has been abandoned, of course, the output part does not need it! It is also allowed to exist only to keep the version backward compatible.
4. SequenceInputStream
It can be thought of as a utility class that reads two or more input streams as one input stream in turn.
5. PrintStream
It can also be considered as an auxiliary tool. You can mainly write data to other output streams, or FileInputStream, and the internal implementation itself is buffered. It is essentially a tool for the comprehensive use of other streams.
Character stream and byte stream conversion
1. Characteristics of the conversion flow:
(1) it is the bridge between character stream and byte stream.
(2) the read byte data can be converted into characters by specified encoding.
(3) the read character data can be converted into bytes by specified encoding.
two。 When to use the conversion stream?
When there is a conversion action between bytes and characters
When the data of a stream operation needs to be encoded or decoded.
3. The specific object embodies:
InputStreamReader: a bridge from bytes to characters
OutputStreamWriter: a bridge from character to byte
These two stream objects are members of the character system, they have the function of transformation, and they are character streams themselves, so it is necessary to pass in byte stream objects when constructing them.
VIII. File class
The File class is an object that encapsulates files and folders in the file system, and you can manipulate files and folders through the idea of objects. The File class stores all kinds of metadata information about a file or directory, including file name, file length, last modification time, readability, getting the path name of the current file, judging whether the specified file exists, getting the list of files in the current directory, creating and deleting files and directories, and so on.
9. RandomAccessFile class
The object is not a member of the stream system, it encapsulates the byte stream, but also encapsulates a buffer (character array) that manipulates the data in the character array through internal pointers. The characteristics of the object:
This object can only manipulate files, so the constructor takes two types of parameters: a. String file path; b.File object.
The object can read and write to the file, and the operation mode can be specified when the object is instantiated.
Note: when the object is instantiated, if the file to be operated does not exist, it will be created automatically; if the file exists and the write data does not specify a location, it will be written from scratch, that is, the original content will be overwritten. Can be used for multithreaded downloads or multiple threads to write data to a file at the same time.
Thank you for your reading, the above is the content of "how to understand JAVA IO flow", after the study of this article, I believe you have a deeper understanding of how to understand JAVA IO flow, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.