Java 1.0 IO System Introduction
1 Java IO version
The IO of the Java library is divided into two parts: input/output.
The early Java 1.0 version of the input system was inputstream and its subclasses, and the output system was outputstream and its subclasses.
The later Java 1.1 version of the IO system was redesigned. The input system is reader and its subclasses, and the output system is writer and its subclasses.
Java1.1 is designed to be redesigned primarily to add internationalization support (that is, support for 16-bit Unicode code added). The IO system, which behaves as Java 1.0, is a byte stream, while the Java 1.1 IO system is character streams.
Byte throttling, which is the smallest data cell in the data stream is 8 bits.
Character stream, the smallest data cell in the data flow is a 16-bit character.
The byte stream does not use buffering when it is operating, and the character streams use buffering. Therefore, character streams are more efficient.
Why is it more efficient to use a buffer? That's because buffering is essentially a memory area, and files are mostly stored on hard drives or NAND flash. Read and write memory faster than read or write the hard disk or NAND flash files on the speed of a lot!
Currently, files are mostly stored in bytes. So in the development, the use of Word throttling is more extensive.
2 Comparison of IO classes for Java 1.0 and Java 1.1
Table 01_java 1.0 and Java 1.1 IO base class comparison table
Table 02_java 1.0 and Java 1.1 io adorner comparison table
The Java IO system is divided into basic classes and adorners because the basic class mainly divides streams into files, strings, and so on, and adorners are designed to implement "decorator Mode" (Refer to "4 decorator mode").
3 Java 1.0 IO System Introduction
Because Java 1.0 and Java 1.1 IO systems are similar in principle and usage. Therefore, the following is mainly the Java 1.0 IO system description.
3.1 Input stream
InputStream is a superclass of all classes of byte input streams, which mainly includes the following several commonly used subclasses.
(01) Bytearrayinputstream
Uses a byte array as a byte array input stream.
(02) StringBufferInputStream
Use the string as a string input stream. is obsolete and is not recommended for use.
(03) FileInputStream
Use a file or FileDescriptor as a file input stream. When the input stream is created, the parameters passed can be "file name" (String type), "File Object", or "FileDescriptor object" (that is, a handle to a standard input stream, an output stream, and an error stream).
(04) Pipedinputstring
Provides all data bytes to be written to the pipeline output stream. When the input stream is created, the corresponding pipe output can be specified alternately, which means that the corresponding pipe input is connected to the pipe output stream.
(05) FilterInputStream
It inherits and InputStream directly, and is mainly used to implement the decorator pattern (which is described later).
FilterInputStream mainly has two derived classes "DataInputStream" and "Bufferedinputstream". DataInputStream provides a variety of interfaces for reading basic types and strings such as Byte, char, int, and Bufferedinputstream provides buffering functionality.
3.2 Output stream
OutputStream is a superclass of all classes of byte output streams, which mainly includes the following several commonly used subclasses.
(01) Bytearrayoutputstream
The byte array output stream in which the data is written to a byte array. The buffer automatically grows as the data is being written. You can use Tobytearray () and toString () to get the data.
(02) FileOutputStream
A file output stream is an output stream that is used to write data to a file or FileDescriptor.
Whether a file is available or can be created depends on the underlying platform. In particular, some platforms allow only one fileoutputstream (or other file write object) at a time to open the file for writing. In this case, if the file involved is already open, the construction method in this class will fail.
(03) Pipedoutputstring
Pipeline output can be linked to the pipeline input stream to create a communication pipeline. The pipeline output stream is the sending end of the pipe. Typically, data is written to the PipedOutputStream object by a thread and is read by other threads from the pipedinputstream of the connection.
It is not recommended that you attempt to use a single thread for both objects, as this could cause the thread to deadlock. If a thread is reading a data byte from a connected pipe input stream, but the thread is no longer active, the pipe is considered to be in a corrupted state.
(04) Filteroutputstream
It inherits and OutputStream directly, and is mainly used to implement the decorator pattern (which is described later).
Filteroutputstream mainly has two derived classes "DataOutputStream" and "Bufferedoutputstream". DataOutputStream provides a variety of interfaces for writing basic types and strings such as Byte, char, int, and Bufferedoutputstream provides buffering functionality.
4 Decorator Mode
Decorator, decorative mode, also known as wrapper. Its main function is to dynamically extend the functionality of a class.
Javaio system includes "file Flow", "string Stream", "byte stream", "buffer stream" and so on. If, we also need a variety of data flow functions, such as input stream and buffer flow. In the way of inheritance, the quantity is too considerable. In order to solve this problem, the decorator mode realizes the dynamic extension, that is, it dynamically gives a class "decoration" on some functions while running. Like what:
InputStream input = Newbufferedinputstream (new FileInputStream ("test.txt"));
This is to decorate the fileinputstream into a bufferedinputstream, so that it has a buffering function.
5 JAVA IO Example
Copy Code code as follows:
Package Com.skywang;
Import java.io.*;
public class iotest{
Publicstaticvoidmain (string[] args) throwsioexception {
try {
File f=newfile ("D:123.txt");
OutputStream out =
New Bufferedoutputstream (
New FileOutputStream (f));
String str= "helloskywang!";
Byte[]b=str.getbytes ();
for (int i = 0; i < b.length; i++) {
Out.write (B[i]);
}
Out.close ();
}catch (FileNotFoundException e) {
E.printstacktrace ();
}catch (SecurityException e) {
E.printstacktrace ();
}
}
}