Thorough understanding of the Java IO system

Source: Internet
Author: User
Tags implement readline stdin thread

Transfer from--bean Technology Network (http://www.ddvip.net/program/java/index1/61.htm)

A Input and OUTPUT1. The stream represents any data source that is capable of producing data, or any receiving source capable of receiving data. In Java io, all of the stream (including input and out stream) includes two types: 1.1 byte-oriented stream with byte-oriented stream, which indicates that the message is read from or to the stream in bytes. The byte-oriented stream includes the following types: 1 input stream:1) Bytearrayinputstream: Use a buffer in memory as a InputStream using 2) StringBufferInputStream: Take a String object as a InputStream3) FileInputStream: A file as a inputstream, implementation of the file read operations 4) PipedInputStream: Implements the concept of pipe, using 5 in the main thread Sequenceinputstream: merging multiple inputstream into one InputStream2) out stream1) Bytearrayoutputstream: Storing information in a buffer in memory 2) FileOutputStream: Put the information in the file 3 PipedOutputStream: Realize the concept of pipe, the main thread to use 4) Sequenceoutputstream: Combine multiple outstream into a OutStream1.2 stream with Unicode character-oriented stream, Represents the reading or writing of information from a stream to a stream in Unicode characters. Unicode-oriented stream includes the following types: 1 Input Stream1) CharArrayReader: 2 with Bytearrayinputstream StringReader: With StringBufferInputStream corresponds 3) FileReader: With FileInputStream corresponds 4) Pipedreader: With PipedInputStream corresponds 2) out STREAM1) Chararraywrite: corresponding to the Bytearrayoutputstream 2) stringwrite: No corresponding byte-oriented stream3) FileWrite: With fiLeoutputstream corresponds to 4) Pipedwrite: corresponds to the PipedOutputStream


The character-oriented stream basically has a byte-oriented stream corresponding to it. Two corresponding classes implement the same functionality, the word is in the operation of the different direction. such as CharArrayReader: and Bytearrayinputstream function is to use a buffer in memory as a inputstream, the difference is that the former each time from memory to read a byte of information, the latter each time from memory read a character. 1.3 Conversion between two InputStreamReader stream and Outputstreamreader: Converts a byte-oriented stream into a character-oriented stream. 2. Stream Add Attribute 2.1 "add attributes for stream" Using the Java operating IO API described above, we can do whatever we want to do. But by FilterInputStream and Filteroutstream subclasses, we can add attributes to the stream. Here is an example to illustrate the role of this function. If we're going to write data to a file, we can do this: Fileoutstream fs = new Fileoutstream ("Test.txt"), and then you can invoke write () by the resulting FS object function to write data to and from the Test.txt file. However, if we want to implement the function of "first caching the data to be written to the file in memory and then writing the data in the cache to the file", none of the APIs will suffice for our needs. But by FilterInputStream and Filteroutstream subclasses, add the functionality we need for fileoutstream. The various types of 2.2 FilterInputStream are 2.2.1 used to encapsulate byte-oriented InputStream1) DataInputStream: Reading basic types (int, char, etc.) data from the stream. 2) Bufferedinputstream: Use buffer 3) Linenumberinputstream: Records the number of rows in the input stream, and then calls Getlinenumber () and setlinenumber (int) 4) Pushbackinputstream: Rarely used, generally used for compiler development 2.2.2 used to encapsulate character-oriented InputStream1) does not have a corresponding class for DataInputStream. Unless you want to use ReadLine () instead of BufferedreadEr, otherwise using DataInputStream2) BufferedReader: Corresponds to the Bufferedinputstream 3) LineNumberReader: With the Linenumberinputstream corresponds 4) Pushbackreader: Various types 2.2.3 corresponding to the Pushbackinputstream 2.3 filteroutstream for encapsulating byte-oriented OutputStream1) Dataioutstream: Output basic type (int, char, etc.) data to stream. 2) Bufferedoutstream: Using buffer 3) PrintStream: Generating formatted output 2.2.4 used to encapsulate character-oriented OutputStream1) Bufferedwrite: with corresponding 2 printwrite: with corresponding 3 . RANDOMACCESSFILE1) You can read and write files by Randomaccessfile Objects 2 when you produce an object, you can indicate the nature of the file you want to open: R, read only, W, write only, RW can read/write 3, and skip directly to the location specified in the file 4. An example of I/O application import java.io.*;p ublic class testio{public static void Main (string[] args) throws ioexception{//1. Reads data from a file in a behavior unit bufferedreader in = new BufferedReader (New FileReader ("F:\\nepalon\\testio.java")); string s, s2 = new string () while ((s = in.readline ())!= null) s2 + = + "\ n"; In.close ();
1b. Receive keyboard input BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in)); System.out.println ("Enter a Line:"); System.out.println (Stdin.readline ());

2. Reads 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. Format input from memory try{datainputstream in3 = new DataInputStream (New Bytearrayinputstream (S2.getbytes ()); while (true) System.out.println ((char) in3.readbyte ()); }catch (eofexception e) {System.out.println ("End of Stream");}

4. Output to file Try{bufferedreader in4 =new bufferedreader (new StringReader (S2)); PrintWriter out1 =new PrintWriter (New BufferedWriter ("FileWriter f:\\nepalon\\")); int testio.out = 1; while (s = in4.readline ())!= null) out1.println (linecount++ + ":" + s); Out1.close (); In4.close (); Atch (Eofexception ex) {System.out.println ("End of Stream");}

5. Storage and recovery of data try{dataoutputstream out2 = new DataOutputStream (New Bufferedoutputstream ("FileOutputStream \ Data.txt ")) out2.writedouble (3.1415926); Out2.writechars (" \nthas was pi:writechars\n "); Out2.writebytes (" Thas Pi:writebyte\n "); Out2.close ();D atainputstream in5 =new datainputstream (New Bufferedinputstream ( "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. Through 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.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 ();} Explanation of the code (in the area): in Zone 1, when the file is read, the contents of the file are read to the cache, and when In.readline () is invoked, the data is read from the cache in characters ("cached byte reads"). 1b zone, the standard IO (system.in) is converted to a character-oriented stream, and then BufferedReader encapsulated, because it wants to read data from the standard IO (keyboard) in cache byte reading mode. In Zone 2, you want to read data from a string object as a character, so you want to produce a stream of stringreader type. In zone 4, when reading data to string Object s2, the data in the object is stored in the cache and then read from the buffer; When the Testio.out file is operated, the formatted information is first exported to the cache, and the information in the cache is exported to the file. In zone 5, when the output of the Data.txt file is is the basic type of data output housing cache, and then the data in the cache output to the file, the file read operation, the file read the data into the cache, and then from the cache in the form of basic types of reading. Note the in5.readdouble () line. Because the first writedouble () is written, so that it is displayed correctly. You also want to read in the form of a basic type. Area 6 is the operation of the file through the Randomaccessfile class


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.