What is IO at the java level?

 1. What is IO

IO is to convert the data in the disk into a data stream and read it into the memory

               Convert the data in memory into a data stream and write it to disk

  I/O operations in Java mainly refer to using Java for input and output operations. All I/O mechanisms in Java are based on data streams for input and output, and these data streams represent the flow sequence of character or byte data. Java's I/O streams provide a standard way to read and write data. Any object that represents a data source in Java provides methods for reading and writing its data in a data stream.  

      Java.io is the main package for most dataflow-oriented input/output classes. In addition, Java also provides support for block transmission, and block IO is used in the core library java.nio.

  The advantage of streaming IO is that it is easy to use, but the disadvantage is that it is less efficient. Block IO is very efficient, but programming is more complicated. 
      Java IO model:
      Java's IO model is very well designed. It uses the Decorator mode to divide Streams by function. You can dynamically assemble these Streams to obtain the functions you need. For example, if you need a buffered file input stream, you should combine FileInputStream and BufferedInputStream. 

Two: The basic concept of data flow

There are three storage methods for data on the computer, one is external storage, the other is internal memory, and the other is cache. For example, the hard disk, disk, and U disk on the computer are all external storage, and there is a memory stick on the computer, and the cache is in the CPU. External storage has the largest amount of storage, followed by internal memory, and finally cache. However, the reading of data from external storage is the slowest, followed by internal memory, and cache is the fastest. Here is a summary of reading data from external storage to memory and writing data from memory to external storage. For the understanding of memory and external storage, we can simply understand it as a container, that is, external storage is a container, and memory is another container. Then how to read the data in the external storage container to the memory container and how to store the data in the memory container to the external storage?

     The abstraction of input and output in network java is called stream, just like a water pipe, connecting two containers. The one that reads data from external storage to memory is called an input stream, and the one that writes data from memory to external storage is called an output stream.

Summary: A data stream is an ordered set of data sequences of bytes or characters with a starting point and an ending point. Including input stream and output stream.

Three: How to choose which stream to use

It is a hard disk file: File++:

             Read: FileInputStream,, FileReader, 

              Write: FileOutputStream, FileWriter
is an array for memory

                  byte[]: ByteArrayInputStream, ByteArrayOutputStream
                  is char[]: CharArrayReader, CharArrayWriter
is String: StringBufferInputStream (obsolete, because it can only be used for each character of String is an 8-bit string), StringReader, StringWriter
is network: use Socket flow

Is the keyboard: Read with System.in (which is an InputStream object), print with System.out (which is an OutoutStream object)

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/131472690