It’s normal for beginners to think that IO streams are complex, but ultimately they don’t understand the design philosophy of the JavaIO framework:

1, before learning IO stream, we wrote programs, are in memory with their own play. For example, you declare a variable, you create an array, you create a collection, you simulate a linked list, you use some of the usual apis, now that you think about it, are you just playing around in memory? The computer consists of an arithmetic unit, a controller, a memory unit, an input device, and an output device. So your previous work is just enough for your program to work with memory and CPU, what if you need to work with external devices? Like the keyboard, the monitor, the most common peripherals: the hard drive?

2, so programming languages must provide ways for programs to interact with external devices. That’s where the IO framework comes in. We need to interact with external devices. So how does a computer interact with the outside world? It’s easy to think of: data cables. What’s going on in the wires? In one word: bitstream. Bit is a homonym for bit, the computer word for “bit”, which stands for 0 or 1. One bit or one bit, which is a zero or a one. But, after all, 0 or 1 doesn’t mean anything, so the more common basic unit for computers is the byte, which is a piece of data made up of eight zeros or ones. The above is a simple explanation of the origin of bitstream.

3. Since computers interact with the outside world with input and output information using bits stream, it is easy to imagine the origin of the IO stream name. The data in and out is like a stream. We can think of any input/output operation between a peripheral device and memory as requiring an input/output stream (IO stream), in this case a bit stream. These external equipment, including, the keyboard (standard input device), display (standard output device), audio, or even the game you play games with the handle, and all kinds of sensor signal, can be called external devices, and data interaction between these devices, obviously can’t learn before those arrays, collections, commonly used class, String and so on. Instead, it relies on classes that interact with data from the outside world. What does the data exchange depend on? That is, bit stream, or IO stream.

4. So, if you want to learn IO streams, you have to learn for a particular I/O device. Which I/O devices are most common? Hard drives, of course. Hard disk here can also be understood as a file system. (Java programs run on the APPLICATION software JVM of an operating system platform. In fact, Java programs are not visible to the hard disk, but to the file system provided by the operating system, so it can be directly understood as the file system.) Therefore, when we learn IO streams, we are basically learning how Java operates on file systems. In addition to file systems, we can also learn about Java operating standard input and output devices such as System.in and System.out.

5, know the direction of learning, is to use Java to operate the File system, so the first thing to learn is the representation of the File, that is, the File class. Next, we’re going to manipulate files, and while most of our operations involve manipulating file systems, the concept of IO streams is not limited to manipulating files, as I mentioned earlier. We need to be able to manipulate all inputs and outputs, so the API provides two top-level abstract classes that represent all outputs: InputStream and OutputStream. Moreover, these two classes represent the input and output of bytes. So, when we want to manipulate files, we need specific IO classes that operate on the file system, so we need to learn about FileInputStream and FileOutputStream, which are file input and output bytes.

6, after learning file IO byte stream, we will find that sometimes it is not very efficient to use, for specific reasons check the source code. Just a word of caution: stream objects with buffers can be read one buffer at a time. Stream objects without buffers can only be read byte by byte, which is very inefficient. So you have BufferedPutStream and BufferedOutputSteam.

7. When it comes to operating files, we have to mention the classification and encoding format of files. Files are divided into binary files and text files, binary files are not understood with notepad, their encoding format is special, such as PDF files, EXE files. Notepad open posterity can understand only plain text files, we deal with files, it is inevitable to deal with some text files. It’s easier if you’re an English speaker, because they use common characters that can be represented in an ASCII table, and a letter in a byte. But apparently, for non-English speakers, the size of a single byte does not account for all their words. Therefore, people need to have classes that can handle characters, or that provide the ability to convert input bytes into characters and output characters into bytes that computers can recognize. So, you need two conversion streams: InputStreamReader and OutputStreamWriter. The purpose of these classes is to stream bytes into character streams and characters into byte streams. However, these two streams need to be used on top of existing byte streams, and the design pattern used in these streams is often referred to as decoration pattern.

8. Back to the file system, the most common thing we’re dealing with is the file system. Can we sometimes read text files in a convenient way? Of course, they thought of it and provided it for you. FileReader and FileWriter are two stream objects that directly convert files into read and write streams. Saves you the step of creating a byte stream and then setting up a conversion stream. If you look at the names, which are actually very graphic, Reader and Writer, they tell you that “reading and writing” are both “people can do,” that is, they represent streams of characters. In the same way as InputStreamReader and OutputStreamWriter, we refer to converting a byte stream to readable and converting a byte stream to writable.

BufferedReader and BufferedWriter are required to implement efficient character streams on both readers and writers.

10, speaking of here, the general idea of IO flow has said about the same, do you feel that those chaotic classes before, now know their role and design ideas, a little clearer? It’s easy to remember that byte streams are the foundation, character streams are enhancements to character capabilities, and operating File systems have a ready-made File beginning class. In addition, there are many classes that are “in-memory and playable” such as ByteArrayReader/Writer, PipedWriter/Reader, and other classes that have some functionality such as serialized streams, data input/output streams, and so on. Use again specific analysis can be. I’m going to leave it here.

The attached:

Byte stream

Characters of the flow