preface
You will learn the following
- Understand Java L /O systems.
- Proficient in I/O programming using related classes and interfaces in the java.io package.
- Master Java L /O design principles and design patterns used.
Relevant concepts
The concept of flow
- Java programs use streams to complete input/output. Streams are abstractions that produce or consume information. The I/O system that flows through Java links to physical devices. All streams behave the same way, although the physical devices to which they are linked are different. In this way, the same input/output classes and methods apply to all types of peripherals. This means that one input stream can abstract many different types of input: from a disk file, from a keyboard, or from a network socket. Similarly, an output stream can be output to a file console, disk file, or connected network. Streams are a clean way to handle input/output, for example, without requiring code to understand the difference between a keyboard and a network. The implementation of flows in Java is inside the class hierarchy defined by the java.io package.
Input/output streams
- When I/O, data flows over a communication channel. The so-called “stream” refers to the starting point and ending point of all data communication channels. The channel of information is a data stream. Any channel through which data flows from one place to another is called a data flow.
- Input/output is relative to the program. Programs play two roles in using data: one as source and one as destination. When a program is the source, or provider, of a data flow, the data flow is an “output data flow” to the program (data flows out of the program). If the program is the endpoint of a data flow, the data flow is an “input data flow” to the program (data flows from the program to the program).
- There are two basic types of streams: Input streams and Output streams. An object from which a series of bytes can be read is called an input stream. An object to which a series of bytes can be written is called an output stream.
Input/output classes
- More than 60 classes (streams) are provided in the java.io package.
- Functionally, they fall into two broad categories: input streams and output streams.
- In terms of stream structure, it can be divided into byte stream (byte processing unit or byte oriented) and character stream (character processing unit or character oriented).
- The input and output streams of the byte stream are based on two abstract classes, InputStream and OutputStream. The input and output operations of the byte stream are implemented by subclasses of these two classes. A character stream is a stream of input and output processing in character units.
- Character stream input and output are based on the abstract classes Reader and Writer.
Byte stream and character stream
- There are two types of streams: byte streams and character streams.
- Byte streams provide a convenient way to handle byte input and output. Such as reading or writing binary data using a byte stream.
- Character stream provides convenience for input and output processing of characters. They use a common coding standard and can therefore be internationalized. Of course, there are situations where a character stream is more efficient than a byte stream.
Need to declare: at the lowest level, all input/output is in bytes. Character-based streams only provide a convenient and efficient way to process characters
Logic for reading and writing data
- The input stream.readLogic of data
- open a stream
- while more information
- read information
- close the stream
- The output stream.writeLogic of data
- open a stream
- while more information
- write information
- close the stream
Streams can be either output or input only. Streams must be open, read/write, and close.
The classification of the flow
- Node flow: A stream class read and written from a specific place, such as a disk or an area of memory. Connect directly to the source of the data.
- Filtering flow:Using node flowsAs input or output. Filtering flow isCreated using an existing input stream or output stream connection. Manipulating the flow of nodes, such as wrapping it. Into buffers and so on.
InputStream
- InputStream contains a set of methods required by byte input streams to perform the basic function of reading human data from input streams. When a Java program needs data from a peripheral, it can create an object of the appropriate InputStream subclass type to complete the connection with the peripheral according to the different forms of the data. (Node stream) And then call the specific input method that executes this stream object to realize the input operation of the corresponding peripheral.
- InputStream subclass objects naturally inherit the methods of InputStream. Common methods include: read() to read data, available() to obtain the number of bytes in the input stream, skip(), reset(), mark() to locate the input position pointer, and so on.
OutputStream
- Three basicWrite a method
- Abstract void write(int b): Writes a byte to the output stream.
- Void write(byte[] b): writes all bytes in group B to the output stream.
- Void write(byte[] b, int off, int len) : writes len bytes of data starting from offset OFF in group B to the output stream
- Other methods void flush(): Flushes the output stream, forcing the output bytes in the buffer to be written out.
- OutputStream is an abstract class that defines the stream byte output pattern. All methods of this class return a void value and raise an IOException in case of an error.
Links to I/O streams
- Filtering a stream is almost like enhancing a node stream. Turn it into a buffer, and turn it into a specific data type.
Design principles for Java I/O libraries
- Java’s I/O library provides a mechanism called chaining that links one stream end to end with another to form a stream pipeline. This mechanism is actually an application of a design pattern called the Decorator.
- By linking streams, you can dynamically add functionality to a stream, which is acquired dynamically by combining some of the basic functionality of a stream.
- Fetching one I/O object often requires generating multiple I/O objects, which is why the JavaI/O library is not easy to master, but the use of the Decorator pattern in the I/O library gives us implementation flexibility.
Decorator pattern
- Do not enhance the functionality of objects through inheritance.
- Decorators inherit from the object being decorated (enhanced) and are abstract.
- The decorator has the object to be decorated
- Decorator overrides (enhances) the decorator’s method, usually calling the decorator’s method in the overridden method.
- Pay attention to the point
- Decorator patterns typically use abstract classes
- The decorator is the reencapsulation of the decorator
new BufferedInputStream(new ByteInputStream());//Buffered is the decorator and ByteInput is the decorator
Copy the code