Learning Java, I have to mention JavaIO streams. A stream is a sequential set of bytes with a starting point and an end point. It is a general term or abstraction for data transfer. In other words, the data transmission between two devices is called stream. The essence of stream is data transmission. According to the characteristics of data transmission, streams are abstracted into various classes to facilitate more intuitive data operation. Next I will talk about a JavaIO stream in detail, you remember to collect after reading oh!

Classification of I/O streams

  1. According to the processing data type is divided into: character stream and byte stream.
  2. According to the different data flow, it is divided into input stream and output stream.

Character stream and byte stream

The origin of character streams: because of the differences in data encoding, there are stream objects that can operate on characters efficiently. Essentially, when reading a byte stream, it looks up the specified code table. The difference between a byte stream and a character stream

  1. The units of read and write are different: byte stream is in bytes (8 bits), and character stream is in characters. The characters are mapped according to the code table. Multiple bytes may be read at a time.
  2. Processing objects are different: byte streams can handle all types of data (such as pictures, AVI, etc.), while character streams can only handle character types of data.
  3. The byte stream itself does not use the buffer; it is operated directly by the file itself; The buffer is used by the character stream to manipulate the file. We will verify this below.

Conclusion: Byte stream is preferred. First of all, all files on the hard disk are transferred or saved in bytes, including pictures and other content. But characters are formed only in memory, so byte streams are widely used in development.

Input stream and output stream

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

Java Flow class diagram structure:

1. Input the byte stream InputStream

Definition and structure description:

From the inheritance diagram of the input byte stream, we can see that:

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 Byte arrays, StringBuffers, and local files, respectively. PipedInputStream reads data from a pipe shared by other threads. The knowledge related to Piped will be separately introduced later.

ObjectInputStream and all subclasses of FilterInputStream are decorator streams (the protagonists of decorator mode). The FileInputStream class can create an object with a String pathname, FileInputStream(String name). The DataInputStream must decorate a class to return an object DataInputStream(InputStream in).

2. Output an OutputStream

Definition and structure description:

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

OutputStream is the parent class of all output byte streams. It is an abstract class.

ByteArrayOutputStream and FileOutputStream are two basic media streams that write data to Byte arrays and local files, respectively. A PipedOutputStream writes data to a pipe shared by other threads,

ObjectOutputStream and all subclasses of FilterOutputStream are decorator streams. The specific example corresponds to InputStream.

3. Mapping diagram of input and output of byte stream

The blue part is the main corresponding part, and the red part is the non-corresponding part. The purple dotted lines indicate that these flows are generally used together. As you can see, byte streams in Java IO are extremely symmetric. Philosophically speaking, “being and being”, let’s look at some of the less symmetric classes in these byte streams.

4. Several special input stream class analysis

LineNumberInputStream

When the data is read from the stream, the corresponding line number is obtained, and when and where the branch is determined by the class change initiative, rather than having such a line number in the original. There is no corresponding section in the output section, so we could have created our own LineNumberOutputStream, with a baseline line number for the initial write and a line number for the next line each time we see a newline, which seems ok. Seems even more out of the loop.

PushbackInputStream

Its function is to look at the last byte and put it in the buffer if you are not satisfied. Mainly used in the compiler syntax, lexical analysis part. The BufferedOutputStream in the output part does almost the same thing.

StringBufferInputStream

Has been Deprecated and should not appear in the InputStream section itself, mainly because strings should be within the scope of character streams. It’s deprecated, and it’s not necessarily needed for the output! It is also allowed to exist only to maintain version backwards compatibility.

SequenceInputStream

Think of it as a utility class that reads two or more input streams as if they were a single input stream. It can be removed from the IO package without affecting the structure of the IO package at all, but makes it more “pure” — pure Decorator mode.

PrintStream

You can also think of it as an auxiliary tool. You can mainly write data to other output streams, or to a FileInputStream, which is internally buffered. It is essentially a tool for the combined use of other streams. The same can kick out IO package! System.err and system. out are instances of PrintStream!

5. Character input stream Reader

Definitions and Instructions:

You can see from the inheritance diagram above:

  • Reader is the parent class of all input character streams and is an abstract class.
  • Charreaders and StringReaders are two basic media streams that read data from Char arrays and strings, respectively. PipedReader reads data from a pipe shared with other threads.
  • BufferedReader is obviously a decorator, and it and its subclasses decorate other Reader objects.
  • FilterReader is the parent class of all custom concrete decorator streams. Its subclass PushbackReader decorates Reader objects by adding a line number.
  • The InputStreamReader is a bridge between byte streams and character streams that converts byte streams into character streams. FileReader is one of the most common utility classes to do this, and its source code clearly uses a method to convert a FileInputStream into a Reader. We can get some tricks out of this class. The Reader classes are used in the same way as the InputStream classes. The correspondence between Reader and InputStream will follow.

6. Character output stream Writer

Definitions and Instructions:

You can see in the diagram above:

  • Writer is the parent class of all output character streams. It is an abstract class.
  • CharArrayWriter and StringWriter are two basic media streams that write data to Char arrays and strings, respectively.
  • The PipedWriter writes data to the pipe shared by other threads.
  • BufferedWriter is a decorator that provides buffering for writers.
  • PrintWriter is very similar to PrintStream, and its function and use are very similar.
  • OutputStreamWriter is a bridge between OutputStream and Writer, and its subclass FileWriter is a concrete class that implements this function (see SourceCode for details). The functionality and usage are very similar to OutputStream.

7. Correspondence between input and output of character stream and conversion between character stream and byte stream

Characteristics of conversion flow:

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

  2. Byte data read can be converted to characters through the specified encoding

  3. Can be read to the character data after the specified encoding converted to bytes

When to use a transformation stream?

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

  2. Stream operation data needs to be encoded or decoded.

Concrete object embodiment:

  1. InputStreamReader: byte to character bridge

  2. OutputStreamWriter: A character-to-byte bridge

These two stream objects are members of the character system, they are conversions, and they are character streams themselves, so they need to be passed in byte stream objects during construction.

8. The File types

The File class is an object that encapsulates files and folders in the File system. You can manipulate files and folders through the idea of objects. The File class stores metadata information about files or directories, including File name, File length, last modification time, readability, obtaining the path name of the current File, determining whether the specified File exists, obtaining the list of files in the current directory, and creating or deleting files and directories.

Coding problems with high-level concepts of Java IO streams

Serialization of objects

Object serialization is a way of turning an object into a binary data stream.

For a class to be serialized, it must implement the Java.io.Serializable interface. This interface doesn’t have any methods, though, just like the previous cloneable interface. When this interface is implemented, it indicates that the class has the ability to be serialized.

Note: Objects of classes declared by the Serializable interface will be serialized, but if you want to customize the serialized content, you need to implement the Externalizable interface.

When a class uses the Externalizable interface, the class must have a constructor that takes no arguments. If it does not, an exception will be generated during construction because the constructor that takes no arguments is called by default when the class is unsequenced.

The Externalizable interface can also be used to serialize some of the properties of an object, but it’s a bit more difficult to do so.

When we use the Serializable interface to implement the serialization operation, if a property of an object does not want to be serialized, we can use the transient keyword to indicate that.

As there are other things to do, so I write here, everyone pay more attention and support, I will try my best to share better things to all my friends who support me!