Java Io stream learning Summary

Source: Internet
Author: User
From: www.chinaitlab.com: 18 times

Provided by yangyi at 22:00:02 on

I. Three types of Io streams

1. stream direction: input stream and output stream

2. Data Units of streams are divided into byte streams and byte streams.

3. Different stream-based functions are divided into node stream and processing stream.

Ii. Four abstract classes of Io streams:

Internal stream: reader writer

Byte stream: inputstream (read data)

Outputstream (write data)

III. Basic Method of inputstream

Int read () throws ioexception reads a byte and returns it as an integer.

Void close () throws ioexception close the stream to release memory resources

Long SKIP (long n) throws ioexception skips n Bytes and does not read

Iv. Basic Methods of outputstream

Void write (int B) throws ioexception writes a byte of data to the output stream

Void flush () throws ioexception writes all the buffered data in the output stream to the destination.

5. Basic writer Methods

Void write (INT c) throws ioexception writes a character data to the output stream

Void write (string Str) throws ioexception writes characters in a string to the output stream

Void write (string STR, int offset, int length)

Writes the length of a string starting from offset to the output stream.

Void flush () throws ioexception

Write all the buffered data in the output stream to the destination.

6. Basic reader Methods

Int read () throws ioexception reads a character and returns it as an integer. If the return value is-1, it is at the end of the input stream.

VII. node stream type

8. The fileinputstream and fileoutputstream inheritance base classes for accessing files are used to input and output bytes to files.

9. The filereader and filewriter inheritance base classes for accessing files are used to input and output characters to files.

---- The second parameter of the output stream in the constructor can be set to true, meaning that it is entered after an existing file.
---- This type of stream throws filenotfoundexception and needs to be captured.

 

10. Buffer stream: the buffer stream needs to be mounted to the corresponding node stream, improving the read/write efficiency.

The construction method of this processing stream must be passed to the corresponding base class type

Bufferedreader: Provides the Readline Method for colleges to read a single string.

Bufferedwriter: Provides newline for writing a line separator, that is, line feed.

Bufferedinputstream is of little use

Bufferedoutputstream is of little use

11. Conversion stream. Great use!

When constructing a conversion stream, you can specify its encoding set.

Inputstreamreader needs to be connected with inputstream

Outputstreamwriter needs to be connected with outputstream

Example: outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream (file path );

Method example: OSW. getencoding (); get the stream encoding method

12. Data streams and byte array streams:

Data streams are mainly used to access Java raw data types such as long and Boolean.

The data stream is a byte stream.

Datainputstream needs to be connected with inputstream

Dataoutputstream needs to be connected with outputstream

Datainputstream method: readboolean () readint () read ...... ()......

Readutf (): a common method for network transmission to read a unicode string

The dataoutputstream method and datainputstream basically correspond to the write method.

// This constructor is equivalent to inputting content into a byte array.

Bytearrayoutputstream baos = new bytearrayoutputstream ();

// This method returns a byte array for obtaining a byte array.

Baos. tobytearray ();

// This method obtains the number of bytes occupied by the byte array.

New bytearrayinputstream (a byte array ). Available ()

1 bytearrayoutputstream baos =
2 new bytearrayoutputstream ();
3 dataoutputstream dos =
4 New dataoutputstream (baos );
5 {
Function onclick ()
{
This. style. Display = 'none'; codehighlighter=172_558_open_text.style.display = 'none'; codehighlighter=172_558_closed_image.style.display = 'inline'; inline = 'inline ';
}
} "Src =" http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505997.gif "alt =" "align =" TOP "> {
Function onclick ()
{
This. style. Display = 'none'; encoding = 'none'; codehighlighter=172_558_open_image.style.display = 'inline'; codehighlighter=172_558_open_text.style.display = 'inline ';
}
} "Src =" http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505279.gif "alt =" "align =" TOP "> try {
6 dos. writedouble (math. Random ());
7 dos. writeboolean (true );
8 bytearrayinputstream BAIS =
9 new bytearrayinputstream (baos. tobytearray ());
10 system. Out. println (BAIS. Available ());
11 datainputstream Dis = new datainputstream (BAIS );
12 system. Out. println (DIS. readdouble ());
13 system. Out. println (DIS. readboolean ());
14 dos. Close (); DIS. Close ();
15 {
Function onclick ()
{
This. style. Display = 'none'; codehighlighter=582_615_open_text.style.display = 'none'; codehighlighter=582_615_closed_image.style.display = 'inline'; inline = 'inline ';
}
} "Src =" http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505997.gif "alt =" "align =" TOP "> {
Function onclick ()
{
This. style. Display = 'none'; usage = 'none'; codehighlighter=582_615_open_image.style.display = 'inline'; codehighlighter=582_615_open_text.style.display = 'inline ';
}
} "Src =" http://java.chinaitlab.com/UploadFiles_8734/200903/20090303124505279.gif "alt =" "align =" TOP ">}catch (ioexception e ){
16 E. printstacktrace ();
17}

12. Print stream

The print stream only has no input stream for the output stream, and the printwriter and printstream are for character bytes respectively.

The two classes provide the overloaded print and println methods for output of multiple data types.

Output operations of printwriter and printstream will not throw an exception

Printwriter and printstream have the automatic flush function.

---- System. setout (receiving outputstream class): used to set the default output stream of the system.

12. Object stream

Equivalent to C # serialization, which is used to directly write or read an object

The transient keyword is not to serialize this member variable.

The class to be serialized must implement the serializable interface.

Main Methods: writeobject (object); readobject ();

To read data of the object type, you must convert the Data Type

1 import java. Io .*;
2
3 Public class testobjectio {
4 public static void main (string ARGs []) throws exception {
5 t = new T ();
6 T. K = 8;
7 fileoutputstream Fos = new fileoutputstream ("D:/share/Java/IO/testobjectio. dat ");
8 objectoutputstream OOS = new objectoutputstream (FOS );
9 OOS. writeobject (t );
10 OOS. Flush ();
11 OOS. Close ();
12
13 fileinputstream FCM = new fileinputstream ("D:/share/Java/IO/testobjectio. dat ");
14 objectinputstream OIS = new objectinputstream (FCM );
15 t treaded = (t) Ois. readobject ();
16 system. Out. println (treaded. I + "" + treaded. J + "" + treaded. d + "" + treaded. K );
17
18}
19}
20
21 class t
22 implements serializable
23 {
24 int I = 10;
25 Int J = 9;
26 double D = 2.3;
27 transient int K = 15;
28}

 

Link: http://java.chinaitlab.com/base...

Remaining points:

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.

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.