What is IO
IO (Input / Output) is an interface for computer output / output. Java's core library Java.io provides a comprehensive IO interface, including: file read and write, standard device output, and more.
The IO in Java is input to the basis of stream, and all data is written to the output stream by serialization, or read from the input stream. In addition, Java also supports block transmission, which is block IO in the core library java.nio. About NIO we have specialized discussions behind.
The benefits of flow IO are simple and easy, and the disadvantage is that the efficiency is low.
Block IO is efficient, but the programming is more complicated.
Java IO model
Java
The IO model is very good,
It uses the Decorator mode, divided by function, you can dynamically assemble these stream to get the features you need. For example, you need a buffered file input stream, you should use FileInputStream and BufferedInputStream.
Java's IO Braces INPUT / OUTPUT and Reader / Writer, the difference is that Reader / Writer can automatically convert the internal code when reading and writing text. Basically, all IO classes are paired, that is, XXXINPUT has a corresponding XXXOUTPUT.
Java IO tutorial
If you are
Decorator mode is very understandable, you can easily see the IO class structure of Java:
The root interface is InputStream / OutputStream,
The IO class acts as a data source has FileInputStream / FileOutputStream, BytearrayInputstream / ByteaRrayoutputstream, etc.
The IO class that acts as a decorative function has bufferedinputstream / bufferedoutputstream, DataInputStream / DataOutputstream, etc.
They are all inherited decorative interfaces FilterInputStream / filteroutputstream.
When using IO, first create a data source IO, then create a decorative class IO as needed, and the parameters of its constructor are the created data source IO. We use a buffered file input stream as an example, assume that you need to read file "c: /log.txt" from disk:
// Create a FileInputStream: FileInputStream fileInput = new FileInputStream ( "C: //log.txt"); // Create a BufferedInputStream: BufferedInputStream bufferedInput = new BufferedInputStream (fileInput); // get the current file that is bufferedInput buffered Enter flow or further simultaneously as follows:
InputStream Input = New BufferedInputStream (New FileInputStream ("c: //log.txt")); // The INPUT now obtained is a buffered file input stream
After you have a general understanding of Java's IO, we recommend you to look at the tutorial
Introduction to Java I / O and
I / o: Reading and Writing.
Java NIO programming
NIO provides support for block IO, the benefits of block IO are higher efficiency, and Java's NIO will directly call many advanced IO interfaces provided by the operating system, support block transmission, read and write lock, asynchronous IO, etc., the efficiency is very high. NIO's programming model is channel and buffer, it is recommended to read Getting Started with New I / O (
Chinese).