Java IO System

xiaoxiao2021-03-06  72

Thorough understanding Java IO system

Input and Output Stream are represented by any data source that has the ability to output data or any capacity to receive data. In Java's IO, all streams include two types: 1 by byte-oriented stream in byte-oriented Stream, represents bytes, read or read from Stream in bytes or Write information into the stream. Byte oriented stream comprises the following types: input stream: 1) ByteArrayInputStream: a buffer memory to use as InputStream 2) StringBufferInputStream: a String object as the InputStream 3) FileInputStream: to a file as InputStream, Implementing the reading operation of the file 4) PipedinputStream: implements the concept of PIPE, mainly in the thread 5) SequenceInputStream: Multiple InputStream: 1) ByTearRayoutputStream: A buffer in memory into memory District 2) FileOutputStream: PipedputStream: PipedputStream: Implement PIPE concept For the oriented Stream, it means that the information is read or written in the stream in units in Unicode characters. In Unicode character oriented stream comprises the following types: Input Stream: 1) CharArrayReader: corresponds with ByteArrayInputStream 2) StringReader: corresponds with StringBufferInputStream 3) FileReader: corresponds with FileInputStream 4) PipedReader: PipedInputStream corresponding to the Out Stream: 1) CharArrayWrite : Corresponding to byterrayoutputstream 2) StringWrite: Corresponds to byte-oriented Stream 3) FileWrite: PiPedWrite: PiPedWrite: The PiPedoutputStream corresponds to the character-oriented Stream basically paired with it. 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. 3 Two Transformation InputStreamReader and OutputStreamReader between unscrupulous Stream: Convert a byte-oriented stream into a character-oriented Stream. Stream Add Property 1 "Add Property for Stream Add Properties" API to operate IO in Java described above, we can complete any of our best to do. 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 FilterInputStream's various types for encapsulate bytes-oriented InputStream 1) DataInputStream: Read basic types (int, char, etc.) data from Stream. 2) BufferedInputStream: Using Buffer 3) LinenumberinputStream: The number of rows within the input stream can be recorded, then you can call getLineNumber () and setLinenumber (int) 4) PushbackInputStream: Little usage, generally used for compiler development for packaging Character-oriented InputStream 1) There is no class corresponding to DataInputStream. Unless the switch is to be used in BufferedReader readLine (), otherwise the DataInputStream 2) BufferedReader: correspondence with BufferedInputStream 3) LineNumberReader: corresponds with LineNumberInputStream 4) PushBackReader: 3 FilterOutStream types corresponding to the PushbackInputStream for encapsulating a byte-oriented OutputStream 1) DataiOutstream: Outputs Basic Types (INT, CHAR, etc.) data into Stream. 2) BufferedOutstream: Using Buffer 3) PrintStream: Generating Format Output for Packaging Character-oriented OutputStream 1) BUFFEREDWRITE: and CORY 1) PrintWrite: with Corresponding RandomaccessFile 1) You can complete the read and write operations of the file via the RandomaccessFile object 2) When an object is generated, you can specify the nature of the file to be opened: R, read-only; W, only write; RW Readable Writing 3) You can directly jump to a location I / O application specified in the file 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.ready ());

// 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 ();

}

}

About the interpretation of the code (in the zone): 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 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.

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

New Post(0)