Java Io stream Learning

Source: Internet
Author: User

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

Pf36_1.jpg

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

Qqread_main_bg.jpg

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

Qqread_main_bg1.jpg

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.