Java IO System

xiaoxiao2021-03-06  64

1. StreamStream represents any data source that has the ability to output data, or any capacity to receive the received data. 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 includes the following types: 1) Input stream: 1) ByTearRayInputStream: Use a buffer in memory 2) StringBufferInputStream: Take a String object as an InputStream 3) fileInputStream: put a file as INPUTSTREAM, implementation of the file reading operation 4) PipedinputStream: implements the concept of PIPE, mainly in the thread 5) SEQUENCEINPUTSTREAM: Multiple InputStream is merged into an InputStream 2) TearRayoutputStream: put information into memory One buffer 2) FileOutputStream: PipedputStream: PipedputStream: Implement the concept of PIPE, mainly in the thread 4) SEQUENCEOTPUTSTREAM: Multiple outstream merge into an Outstream 1.2 with Unicode Character-oriented Stream STREAM-oriented Stream indicates that the Unicode character is written from the Stream from the stream. In Unicode character oriented stream comprises the following types: 1) Input Stream 1) CharArrayReader: corresponds with ByteArrayInputStream 2) StringReader: corresponds with StringBufferInputStream 3) FileReader: corresponds with FileInputStream 4) PipedReader: corresponds with PipedInputStream 2) Out Stream 1 CHARARRAYWRITE: Corresponds to ByteArrayoutputStream 2) StringWrite: Corresponds to byte-oriented Stream 3) FileWrite: PassputStream Corresponding 4) PiPedWrite: The PiPedoutPutStream corresponds to the character-oriented Stream basically pair 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 unsampicated streams InputStreamReader and OutputStreamReader: Convert a byte-oriented Stream into a character-oriented Stream. 2. Stream Add Properties 2.1 "Add Property for Stream Add Properties" to operate the IO's API in the Java described above, we can complete any operation we want to complete. 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 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 encapsulate bytes-oriented InputStream 1) 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 The encapsulation is not classified with the DataInputStream corresponding to the character-oriented InputStream 1. Unless the switch is to be used readLine () BufferedReader, otherwise the DataInputStream 2) BufferedReader: correspondence with BufferedInputStream 3) LineNumberReader: corresponds with LineNumberInputStream 4) PushBackReader: 2.3 FilterOutStream corresponding to the PushbackInputStream 2.2.3 for packaging various types of words to Guided OutputStream 1) 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 OutputStream 1) BufferedWrite: and Correspondence 2) PrintWrite: With the corresponding 3. RandomaccessFile 1) Create Document reading and writing 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 jump to the location specified in the file 4. I An example of / o 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 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 DataInputStream (New ByTearrayInputStream (S2.GetBytes ()); while (true) system .out.println ((cha) r) in3.readbyte ());} catch (eofexception e) {system.out.println ("end of stream");} // 4. Output to file try {buffredreader in4 = new buffreader (new stringReader (S2) PrintWriter Out1 = New PrintWriter (New FileWriter (New FileWriter ("f: // Nepalon /// Testio.out")))); int linecount = 1; while ((s = in4.readline ())! = Null) Out1.Println: " S); OUT1.CLOSE (); in4.close ();} catch (eofexception ex) {system.out.println (" end of stream ");} // 5. Data storage and restoration Try {dataputstream 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.close ()); rf.close (); rf = new randomaccessfile "F: // nepalon // rtest.dat", "rw"); RF.Seek (5 * 8); RF.WR ITEDOUBLE (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): 1 area, when reading the file, first Read the contents of the file to the cache, when invoing IN.Readline (), then read the data (hereinafter referred to as "cache byte reading method" 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.

转载请注明原文地址:https://www.9cbs.com/read-110326.html

New Post(0)