Transfer from - Bean Technology Network (http://www.ddvip.net/program/java/index1/61.htm)
One. INPUT and OUTPUT1. Stream represents any data source that has the ability to output data, or any capacity to receive data receiving source. In Java's IO, all streams include two types: 1.1 Byte-oriented stream in byte-oriented Stream, indicating bytes, read or read from Stream in bytes or Write information into the stream. Byte oriented stream comprises the following types: 1) input stream: 1) ByteArrayInputStream: the use of a buffer memory 2) StringBufferInputStream as InputStream: a String object to a InputStream3) FileInputStream: to a file as InputStream Realize the reading operation of the file 4) PipedinputStream: implements the concept of PIPE, mainly in the thread 5) SEQUENCEINPUTSTREAM: Multiple InputStream is merged into an inputStream2) BYTEARRAYOTPUTSTREAM: put information into a buffer in memory District 2) FileOutputStream: PipedputStream: PipedputStream: Implement the concept of PIPE, mainly in the thread 4) SEQUENCEOTPUTSTREAM: Multiple OutStream merge into an outstream1.2 in Unicode Character-oriented Stream in Unicode Character-oriented Stream indicates that the information is read from the stream in units in Unicode characters to the stream. In Unicode character oriented stream comprises the following types: 1) Input Stream1) CharArrayReader: corresponds with ByteArrayInputStream 2) StringReader: corresponds with StringBufferInputStream 3) FileReader: corresponds with FileInputStream 4) PipedReader: corresponds with PipedInputStream 2) Out Stream1) CharArrayWrite : Corresponds to byterrayoutputstream 2) StringWrite: Corresponding to byte-oriented Stream3) FILEWRITE: PiPedWrite: 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 ().
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. Enter the input bufferedreader stdin = new buffredreader ("Enter a line:"); 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 formatting input from memory Try {DataInputStream in3 = New DataAnputStream (new byterrayinputstream (s2.getbytes ()); while (true) system.out.println ((char) in3.Readbyte ());} Catch (EOFEXCEPTION E) {System.out.println ("End of Stream");
// 4. Output to file try {buffredreader in4 = new buffredreader (new stringReader (new stringReader (new bufferedwriter (New FileWriter (New FileWriter (New FileWriter ("f: // nepalon // Testio.out")))) Int linecount = 1; while ((s = in4.readline ())! = null) Out1.println (LineCount ": S); Out1.close (); in4.close ();} atch (EOFException EX) ) {System.out.println ("end of stream");
// 5 the 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: // Nepalon // data.txt "))))))); BufferedReader In5Br = New BufferedReader (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. 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 ();}} About code interpretation (in the area): In the zone, when the file is read, first read the contents of the file to the cache, when calling in .readline (), the data (hereinafter referred to as "cache byte reading mode") is read from the cache. 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. 6 districts are operated by the RandomaccessFile class