A thorough understanding of Java I/O Systems

Source: Internet
Author: User
I. Input and Output

1. Stream represents any data source capable of producing data or any receiving source capable of receiving data. In Java I/O, all streams (including input and out stream) have two types:

1.1 byte-oriented stream

A byte-oriented stream that reads or writes information to a stream in bytes. Byte-oriented streams include the following types:

1) input stream:

1) bytearrayinputstream: uses a buffer in the memory as an inputstream.

2) stringbufferinputstream: Use a String object as inputstream.

3) fileinputstream: uses a file as inputstream to read the file.

4) pipedinputstream: implements the pipe concept and is mainly used in the process.

5) sequenceinputstream: combines multiple inputstreams into one inputstream.

2) Out stream

1) bytearrayoutputstream: stores information in a buffer zone in the memory.

2) fileoutputstream: stores information in a file.

3) pipedoutputstream: implements the concept of pipe, which is mainly used in the process.

4) sequenceoutputstream: combines multiple outstreams into one outstream.

1.2 Unicode Character-oriented stream

A Unicode-oriented stream that reads or writes information from a stream in units of Unicode characters. Unicode-oriented streams include the following types:

1) input stream

1) chararrayreader: corresponds to bytearrayinputstream

2) stringreader: corresponds to stringbufferinputstream

3) filereader: corresponds to fileinputstream

4) pipedreader: corresponds to pipedinputstream.

2) Out stream

1) chararraywrite: corresponds to bytearrayoutputstream

2) stringwrite: no corresponding byte-oriented stream

3) filewrite: corresponds to fileoutputstream

4) pipedwrite: corresponds to pipedoutputstream

Character-oriented stream basically corresponds to a byte-oriented stream. The functions of the two corresponding classes are the same, and the words are different in operation. For example, chararrayreader and bytearrayinputstream both use a buffer in the memory as inputstream. The difference is that the former reads a byte of information from the memory each time, the latter reads one character from the memory each time.

1.3 conversions between two non-existing stream types

Inputstreamreader and outputstreamreader: convert a byte-oriented stream into a character-oriented stream.

2. Add attributes to stream

2.1 role of "adding attributes for stream"

Using the I/O APIs in Java described above, we can complete any operation we want to complete. However, through the sub-classes of filterinputstream and filteroutstream, we can add attributes for stream. The following is an example to illustrate the role of this function.

If we want to write data to a file, we can do this:

Fileoutstream FS = new fileoutstream(‑test.txt ");

Then, you can use write(writable writable) to write data to the test.txt file. However, if we want to implement the "First cache the data to be written into the file into the memory, then write the data in the cache into the file" function, none of the above APIs can meet our needs. However, we can use the sub-classes of filterinputstream and filteroutstream to add the required functions for fileoutstream.

2.2 various types of filterinputstream

2.2.1 encapsulate byte-oriented inputstream

1) datainputstream: reads data of basic types (such as int and char) from stream.

2) bufferedinputstream: Buffer

3) linenumberinputstream: records the number of rows in the input stream. You can call getlinenumber () and setlinenumber (INT)

4) pushbackinputstream: rarely used and generally used for Compiler Development

2.2.2 encapsulate character-oriented inputstream

1) there is no class corresponding to datainputstream. Unless you use bufferedreader when using Readline (), use datainputstream

2) bufferedreader: corresponds to bufferedinputstream

3) linenumberreader: corresponds to linenumberinputstream

4) pushbackreader: corresponds to pushbackinputstream

2.3 various types of filteroutstream

2.2.3 encapsulate byte-oriented outputstream

1) dataioutstream: outputs basic data types (such as int and char) to stream.

2) bufferedoutstream: Buffer

3) printstream: produce formatted output

2.2.4 character-oriented outputstream Encapsulation

1) bufferedwrite: corresponds

2) printwrite: corresponds

3. randomaccessfile

1) You can use the randomaccessfile object to read and write files.

2) when an object is generated, you can specify the nature of the file to be opened: R, read-only; W, write-only; RW, read/write

3) You can directly jump to the specified position in the file.

4. An example of an I/O Application

Import java. Io .*;

Public class testio {

Public static void main (string [] ARGs)

Throws ioexception {

// 1. read data from a 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 keyboard input

Bufferedreader stdin =

New bufferedreader (

New inputstreamreader (system. In ));

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. Extract formatted 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 (

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 Restoration

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 (

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. Operate the file through 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 ();

}

}

Code explanation (in the unit of Zone ):

In Area 1, when reading a file, first read the file content to the cache, when calling in. readline (), and then read data from the cache in character format (hereinafter referred to as "cache byte read mode ").

In section 1B, to read data from the standard I/O (keyboard) as a cache byte, you must first set the standard I/O (system. in) is converted to a character-oriented stream and then encapsulated by bufferedreader.

In area 2, a stringreader stream is generated to read data from a string object in the form of characters.

In area 4, when reading data from String object S2, the data in the object is first stored in the cache and then read from the buffer; For testio. when operating the out file, the formatted Information is first output to the cache, and then the cached information is output to the file.

In Area 5, when outputting the data.txt file, the basic data type is first output to the House cache, and then the cached data is output to the file. when reading the file, first, read the data in the file to the cache, and then read the data in the basic type from the cache. Note the in5.readdouble () line. Because the first writedouble () is written, it must be correctly displayed. It also needs to be read in the form of basic types.

In Area 6, files are operated through the randomaccessfile class.


Author's blog:Http://blog.csdn.net/caiyi0903/

Related Article

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.