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 call the west.txt file by generating FS objects. Data. 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 for encapsulation of bytes-oriented InputStream1) DataInputStream: Read basic types (int, char, etc.) data from Stream. 2) BufferedInputStream: Using the buffer 3) LINENUMBERINPUTSTREAM: The number of rows within the input stream can then call getLineNumber () and setLinenumber (int) 4) PushbackInputStream: Little use, generally used for compiler development 2.2.2 There is no class corresponding to the DataInputStream in the package with character-oriented InputStream1). Unless the switch is to be used in BufferedReader readLine (), otherwise the DataInputStream2) BufferedReader: correspondence with BufferedInputStream 3) LineNumberReader: corresponds with LineNumberInputStream 4) PushBackReader: 2.3 FilterOutStream corresponding to the various types PushbackInputStream bytes for packaging 2.2.3 Oriented OutputStream1) DataiOutstream: Outputs Basic Types (INT, CHAR, etc.) data into Stream.
2) BufferedOutstream: Using Buffer 3) PrintStream: Generates Format Output 2.2.4 Used to Pack the Character-oriented OutputStream1) BUFFEREDWRITE: and Correspondence 2) PrintWrite: with the corresponding 3. RandomaccessFile1) You can complete the file via the RandomaccessFile object. Read and write operation 2) When an object is generated, indicate the nature of the file to be opened: R, read-only; W, write only; RW Readable Writing 3) You can directly jump to the location specified in the file 4. I / O An example of application iMport java.io. *; Public class testio {public static void main (string [] args) throws ioException {// 1. Read data from a file from a file 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 buffrereader ("Enter a line:"); System.Out.println ("Enter a line:"); system.out.println (stdin); 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 DataNputStream (New ByTearrayInputStream (s2.getbytes ()); while (true) system.out .println ((char) in3.readby ());} catch (eofexception e) {s ystem.out.println ( "End of stream");} // 4 to a file try {BufferedReader in4 = new BufferedReader (new StringReader (s2));. PrintWriter out1 = new PrintWriter (new BufferedWriter (new FileWriter ( "F : // Nepalon // 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 try2 = new dataputstream (New Bufferedputstream (New Bufferedputstream (New Bufferedputstream New FileOutputStream ("f: // nepalon // data.txt"))))))
Out2.writedouble (3.1415926); Out2.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 (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. Pass RandomaccessFile Operation File 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.close ()); 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.print LN ("Value" i ":" rf.readdouble ()); rf.close ();}} About code interpretation (in the area): 1 area, when reading the file, The file content reads into the cache, when invoing IN.Readline (), then read data (hereinafter referred to as "cache byte reading mode) from the cache in characters. 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.