One. INPUT and OUTPUT
1. Stream represents any data source that has the ability to output data, or any capacity to receive the received source. In Java's IO, all streams include both types include two types:
1.1 Byte-oriented Stream
Stream in bytes is read from the stream in bytes, and write information from the stream in bytes. Byte-oriented Stream includes the following types:
1) Input stream:
1) BYTEARRAYINPUTSTREAM: Use a buffer in memory as an InputStream
2) StringBufferInputStream: put a String object as InputStream
3) FileInputStream: Take a file as an InputStream to implement the read operation of the file
4) PipedinputStream: The concept of PIPE is implemented, mainly in the thread.
5) SEQUENCEINPUTSTREAM: Multiple InputStream merged into an InputStream
2) Out stream
1) BYTEARRAYOUTPUTSTREAM: put the information into a buffer in memory
2) FileoutputStream: Deposit information into the file
3) PipedputStream: implements the concept of PIPE, mainly in the thread
4) SEQUENCEOUTPUTSTREAM: Multiple OutStream merged into an Outstream
1.2 Stream-oriented Stream in Unicode characters
STREAM-oriented Stream indicates that the Unicode character is written from the Stream from the stream. STREAM-oriented Stream includes the following types:
1) Input stream
1) ChararrayReader: Corresponds to ByteArrayInputStream
2) StringReader: Corresponds to StringBufferInputStream
3) FileReader: corresponds to FileInputStream
4) PipedReader: corresponds to PiPedInputStream
2) Out stream
1) ChararrayWrite: Corresponds to byteArrayoutputStream
2) StringWrite: Corresponding to the corresponding stream of Bytes-oriented Stream
3) FileWrite: Corresponds to FileOutputStream
4) PiPedWrite: Corresponds to PiPedoutputStream
Based on character-oriented Stream basically correspond to byte-oriented Stream. The two corresponding classes implementation are the same, the word is different during operation. For example, CharaRrayReader: and ByTearRayinputStream uses a buffer in memory as an inputStream, which is different that the former reads a byte information from memory, while the latter reads a character from memory.
1.3 Conversion between two unscrupulous stream
InputStreamReader and OutputStreamReader: Convert a byte-oriented stream into a character-oriented Stream.
2. Stream adds attributes
2.1 "Add Property for Stream"
We can complete any operation we want to do with the API of the IO described above. But through the subclasses of FilterInputStream and FilterOutstream, we can add properties to Stream. The following examples illustrate the function of this function. If we want to write data into a file, we can do this:
FileoutStream Fs = New FileoutStream ("Test.txt");
Then you can call the write () function to write data in the Test.txt file by the generated FS object. However, if we want to implement "first to write the data to the file first caching into memory, then the API above the data is written in the file", the above API does not have a meeting to meet our needs. But add our needs to FileoutStream through FilterInputStream and FilteroutStream.
2.2 FilterInputStream's various types
2.2.1 INPUTSTREAM for encapsulation by byte
1) DataInputStream: Read basic types (int, char, etc.) data from Stream.
2) BufferedInputStream: Using buffers
3) LINENUMBERINPUTSTREAM: records the number of rows within the input stream, then you can call getLinenumber () and setLinenumber (int)
4) PushbackInputStream: Little usage, generally used for compiler development
2.2.2 INPUTSTREAM for encapsulation with characters
1) There is no class corresponding to DataInputStream. Use DataInputStream unless you want to use readline (), otherwise use DataInputStream
2) BUFFEREDReader: Corresponds to BufferedInputStream
3) LinenumberReader: corresponds to LINENUMBERINPUTSTREAM
4) PushbackReader: corresponds to PushbackInputStream
2.3 Various types of FilterOutstReam
2.2.3 OutputStream used to encapsulate bytes
1) DataiOutstReam: Output basic types (int, char, etc.) data into the stream.
2) BufferedoutStream: Using buffers
3) PrintStream: Generate a formatted output
2.2.4 OutputStream used to encapsulate character-oriented
1) BUFFEREDWRITE: and corresponding
2) PrintWrite: with the corresponding
3. RandomaccessFile
1) Read and write operations to files can be completed through the RandomaccessFile object
2) When an object is generated, indicate the nature of the file to be opened: R, read only; W, only write; RW can be read
3) You can jump directly to the location specified in the file.
4. An example of I / O application
Import java.io. *;
Public class testio {
Public static void main (string [] args)
THROWS IOEXCEPTION {
// 1. Read data from one file in behavior units
BufferedReader in =
New BufferedReader
New FileReader ("f: //nepalon/testio.java");
String s, s2 = new string ();
While ((s = in.readline ())! = NULL) S2 = S "/ N";
In.Close ();
// 1b. Receive the input of the keyboard
BufferedReader stdin =
New BufferedReader
New INPUTSTREAMREADER (System.IN));
System.out.println ("Enter a line:");
System.out.println (stdin.readline ());
// 2. Read data from a String object
StringReader in2 = New StringReader (S2);
INT C;
While ((c = in2.read ())! = -1)
System.out.println (CHAR) C);
In2.Close ();
// 3. Remove the format input from the memory
Try {
DataInputStream in3 =
New DataInputStream
New ByteArrayInputStream (S2.GetBytes ()));
While (True)
System.out.println ((char) in3.ready ());
}
Catch (EOFEXCEPTION E) {
System.out.println ("End of Stream");
}
// 4. Output to the file
Try {
BufferedReader in4 =
New BufferedReader
NEW STRINGREADER (S2));
PrintWriter Out1 =
New PrintWriter
New bufferedwriter
New FileWriter ("f: // neipalon // Testio.out"))))))))
INT linecount = 1;
While ((s = in4.readline ())! = null)
Out1.println (LineCount ":" S);
Out1.close ();
In4.Close ();
}
Catch (EOFEXCEPTION EX) {
System.out.println ("End of Stream");
}
// 5. Data storage and recovery
Try {
DataOutputStream out2 =
New DataOutputstream
New bufferedoutputstream
New FileOutputStream ("f: // nepalon // data.txt"))))))
Out2.writeDouble (3.1415926);
Out2.writechars ("/ NTHAS WAS PI: WRITECHARS / N");
Out2.writebytes ("Thas Was Pi: WritebyTe / N);
Out2.close ();
DataInputStream in5 =
New DataInputStream
New BufferedInputStream
New fileInputStream ("f: // narpalon // data.txt")))))))
BufferedReader In5BR =
New BufferedReader
NEW INPUTSTREAMREADER (in5);
System.out.println (in5.Readdouble ());
System.out.println (In5br.Readline ());
System.out.println (In5br.Readline ());}
Catch (EOFEXCEPTION E) {
System.out.println ("End of Stream");
}
// 6. Operating the file through the RandomaccessFile
RandomaccessFile RF =
New randomaccessfile ("f: // nepalon // rtest.dat", "rw");
For (int i = 0; i <10; i )
Rf.writeDouble (i * 1.414);
Rf.close ();
Rf = new randomaccessfile ("f: // nepalon // rtest.dat", "r");
For (int i = 0; i <10; i )
System.out.println ("Value" i ":" RF.Readdouble ());
Rf.close ();
Rf = new randomaccessfile ("f: // nepalon // rtest.dat", "rw");
Rf.seek (5 * 8);
Rf.writedouble (47.0001);
Rf.close ();
Rf = new randomaccessfile ("f: // nepalon // rtest.dat", "r");
For (int i = 0; i <10; i )
System.out.println ("Value" i ":" RF.Readdouble ());
Rf.close ();
}
}
An interpretation of the code (in the area):
In the zone, when the file is read, first read the file content to the cache, when invoing IN.READLINE (), then read data from the cache in characters (hereinafter referred to as "cache byte reading mode" ).
In the 1b area, since the data is read from the standard IO (keyboard) from the standard IO (keyboard) by cache byte reading, the standard IO (system.in) is first converted to the Character-oriented Stream, and then the BUFFEREDReader package is performed.
In the zone, you should read data from a String object in a form of characters, so a StringReader type Stream is created.
In the zone, when the data STRING object S2 reads the data, first put the data in the object, and then read from the buffer; when the Testio.out file is operated, the formatted information is output to In the cache, the information in the cache is output to the file.
In the 5th district, when the data.txt file is output, it is first to output the basic type of data output housing cache, and then output the data in the cache to the file; when the file is read, the data in the file first Read to the cache, then read from the cache in the basic type of form. Note in5.Readdouble (). Because write the first WriteDouble (), in order to be displayed correctly. It is also necessary to read in the form of a basic type.
The 6 district is operated by the RandomaccessFile class.