What is IO?
IO (Input/Output) is the computer Output/Output interface. Java. io provides a comprehensive IO Interface, including file read/write and standard device output.
In Java, I/O is input and output based on a stream. All data is written to the output stream serially or read from the input stream.. In addition, Java also supports block transmission. Block IO is used in the core library java. nio. We will discuss NIO in detail later.
The advantage of stream IO is that it is easy to use and its disadvantage is that it is less efficient.
Block IOHigh efficiency, but complicated programming.
Java IO Model
Java
IO ModelExcellent Design,
It uses the Decorator mode and divides Stream by function.You can dynamically assemble these streams to obtain the functions you need. For example, if you need a buffer file input stream, FileInputStream and BufferedInputStream should be used in combination.
The IO System of Java is divided into two types: Input/Output and Reader/Writer. The difference is that Reader/Writer can automatically convert internal codes when reading and writing texts.Basically, all IO classes are paired, and XxxOutput corresponds to XxxInput.
Java IO tutorial
If you are familiar with the Decorator mode, you can easily see the Java IO class structure: the root interface is InputStream/OutputStream, And the IO classes acting as data sources include FileInputStream/FileOutputStream and ByteArrayInputStream/ByteArrayOutputStream, the IO classes acting as decoration functions include BufferedInputStream, BufferedOutputStream, DataInputStream, and DataOutputStream. They all inherit from the decoration interface FilterInputStream/FilterOutputStream.
When I/O is used, create a data source I/O first, and then create a decoration class I/O Based on the required functions. The constructor parameter is the created data source I/O.Taking the creation of a buffer file input stream as an example, assume that the file "C:/log.txt" needs to be read from the disk ":
// Create a FileInputStream: FileInputStream fileInput = new FileInputStream ("C: // log.txt "); // Create a BufferedInputStream: BufferedInputStream bufferedInput = new BufferedInputStream (fileInput ); // The obtained bufferedInput is a buffer file input stream. |
Or, you can simply enter the following:
InputStream input = new BufferedInputStream ( New FileInputStream ("C: // log.txt ")); // The input obtained now is a buffer file input stream. |
After you have a general understanding of Java I/O, we recommend that you take a look at the tutorial Introduction to Java I/O and I/O: Reading and Writing.
Java NIO Programming
NIO provides block I/O support. The advantage of block I/O is higher efficiency. In addition, Java NIO will directly call many advanced IO interfaces provided by the operating system, supporting block transfer and read/write locking, asynchronous IO and other functions with high efficiency. NIO programming models are channels and buffers. We recommend that you read Getting started with new I/O (Chinese ).