A brief summary of Basic Java Io streams

Source: Internet
Author: User

Three classification methods of one Io stream:
1. Score by Flow Direction:

Input stream: Only bytes of data can be read from it, and data cannot be written to it
Output stream: Only bytes of data can be written to and cannot be read from.

2. Data Types processed by the stream can be divided:

Byte stream: used to process byte data.

Bytes stream: used to process UNICODE character data.

3. The format can be divided:

Node stream (low-level stream): it can read/write data from/to a specific IO Device (such as a disk or network.

Stream processing (Advanced stream): You can connect to and encapsulate an existing stream and use the encapsulated stream function to read and write data.

Four abstract classes of the binary Io stream:
1. Common inputstream classes (read data)
-Low Level

Inputstream
Fileinputstream
Bytearrayinputstream

Pipedinputstream

-Advanced

Datainputstream

Bufferedinputstream

2. Common outputstream (write data)

-Low Level

Outputstream
Fileoutputstream

Bytearrayoutputstream

Pipedoutputstream

-Advanced

Dataoutputstream

Bufferedoutputstream

3. Common reader classes

-Low Level

Chararrayreader

Stringreader

Pipedreader

Filereader
-Advanced

Bufferedreader
Inputstreamreader
Linenumberreader

4. Common writer-related classes
-Low Level

Chararraywriter

Stringwriter

Pipedwriter

Filewriter

-Advanced

Bufferedwriter

Outputstreamwriter

Printwriter

Note: All advanced streams cannot directly interact with I/O devices (disks or networks). They must be based on low-level streams.

Three buffered streams:
Bufferedreader-> Reader

Bufferedwriter

Bufferedinputstream-> inputstream

Bufferedoutputstream

The buffer stream input stream supports the mark () and reset () methods of its parent class.

The mark () method is used to "mark" the current position. Just like adding a bookmarked, the reset () method can return the mark and re-read the data.

Bufferedreader-Readline () -- separated by/R or/n

Bufferedwriter-newline () -- write a row Separator

Bufferinputstream and bufferedoutputstream are rarely used at ordinary times.

4. Basic Method of inputstream
1. Int read () throws ioexception reads a byte and returns it as an integer. If-1 is returned, it indicates that it reads the end of the file.

Int read (byte [] buffer) throws ioexception reads data into a byte array and returns the number of bytes read.

Int read (byte [] buffer, int offset, int length) throws ioexception reads data into a byte array and places it at the specified position of the array offset, use length to specify the maximum number of bytes to read.

2. Void close () throws ioexception close the stream to release memory resources
3. Int available () returns the number of bytes that can be read by Ong.

4. Long SKIP (long n) skips n bytes in the input stream and returns the number of bytes actually skipped.

5. boolean marksupported () determines whether the stream supports the flag function.

6. Void mark (INT readlimit) sets a flag at the current position of the input stream that supports marking.

7. Void reset () returns the previous mark of the stream. Note that the tag function must be supported.

5. Basic Methods of outputstream
1. viod write (int B) throws ioexception writes a byte of data to the output stream.

Void write (byte [] buffer)

Void write (byte [] buffer, int offset, int length)

2. viod flush () writes all the buffered data in the output stream to the destination.

3. Void close () to close the stream

6. Basic writer Methods
1. Void write (INT c) throws ioexception writes a character data to the output stream.
Void write (char [] buffer) writes the characters in the character array buffer to the stream

Void write (char [] buffer, int offset, int length) writes the length characters starting from offset in the character array buffer to the 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.

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

Seven basic methods of Reader
1. Int read () reads a character and returns it as an integer. If-1 is returned, it indicates that it has reached the end of the stream.

Int read (char [] buffer) places the characters read from the stream into the character array buffer, and returns the number of characters read.

Int read (char [] buffer, int offset, int length) places the read characters in the space starting from the specified offset in the character array. A maximum of length characters can be read each time.

2. Void close ();
3. Skip (long N)

4. Void mark (INT readlimit)

5. Void reset ()

8. Access Files
1. fileinputstream and fileoutputstream inherit the base class used to input and output bytes to the file.
2. filereader and filewriter inherit the base class used to input and output characters to the file.
Note: A. The second parameter of the output stream in the constructor can be set to true, indicating that append data is written at the end of the file.
B. This type of stream throws a filenotfoundexception exception.

9. Conversion stream: The main function is to switch byte streams to the swap stream.
1. inputstreamreader needs to be connected to inputstream
2. outputstreamwriter needs to be connected to outputstream

10 data streams and byte array streams:
1. Data streams are mainly used to access the original Java data types, such as long and Boolean.

The data stream is a byte stream.

2. datainputstream needs to be connected with inputstream
Dataoutputstream needs to be connected with outputstream
3. datainputstream method:

-Readboolean (); readint (); read...

-Readutf () is a common method for network transmission to read a unicode string.

4. The dataoutputstream method corresponds to the method in datainputstream.

5. // 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 (byte []). Available ()

11 print stream
1. Print streams only output streams without input streams. printwriter and printstream are for characters and bytes respectively.
2. Provides print and println methods for output of multiple data types.
3. The output operation will not throw an exception.
4. The automatic refresh function can be set for printwriter and printstream.

Twelve object stream
1. serialization, used to directly write or read objects.
2. Attributes modified by the static and transient keywords cannot be serialized.
3. The class to be serialized must implement the serializable interface or externalizable
4. Method:

-Writeobject (Object OBJ)
-Object readobject ();
If you want to obtain the original object type, you need to perform a forced transformation.

Thirteen special file streams (randomaccessfile)
1. randomaccessfile is a special file stream that can be used to search for or insert data anywhere in the file.

2. randomaccessfile implements both the datainput and dataoutput interfaces, so you can use it to read and write files.

3. Constructor

-Randomaccessfile (File file, string Mode)

-Randomaccessfile (string file, string Mode)

4. Use randomaccessfile to append the file content:

Randomaccessfile rf1 = new randomaccessfile ("C: // 1.txt"," RW ");

Rf1.seek (rf1.length ());

Rf1.writebytes (STR + "/N"); // Add/n/R

Rf1.close ();

You can try the random file on your own.

Fourteen Read File matching streams
1. fileinputstream/fileoutputstream
2. filereader/filewriter
3. bufferedreader/printwriter

**************************************
The general principle for deciding which class to use and constructing a process is:
1. First, what is the original data format?
A. binary format (as long as it cannot be determined that it is plain text): inputstream, outputstream and all its subclasses with stream termination

B. plain text format (including English and Chinese characters only or other encoding methods); reader, writer and its subclass

2. Whether input or output is required?
A. Input: reader, a subclass of the inputstream type

B. Output: writer, a subclass of the outputstream type

3. Do I need to use an intermediary?
A. Conversion class from stream to reader: inputstreamreader and outputstreamwriter

B. Conversion of low-level stream to advanced stream

4. What is the source or destination of the data?
A. yes file: fileinputstream, fileoutputstream, filereader, filewriter

B. byte []: bytearrayinputstream, bytearrayoutputstream

C. Char []: chararrayreader, chararraywriter

D. String: stringbufferinputstream, stringreader, stringwriter

E. network data streams: inputstream, outputstream, reader, and writer

5. Do you want to buffer?

A. Four buffer classes are required: bufferedinputstream, bufferedoutputstream, bufferedreader, and bufferedwriter.

6. Do you want to format the output?
A. printstream, printwriter

 

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.