Java IO Stream Learning Summary

Source: Internet
Author: User

Classes or interfaces related to Java stream operations:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/85/80/wkiom1elz5ghw68taaaypfdrd7c760.png "title=" 1.png "alt=" Wkiom1elz5ghw68taaaypfdrd7c760.png "/>

Java Flow class diagram structure:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/85/80/wkiol1elz7ri7imyaady7e3b_-c694.png "title=" 2.png "alt=" WKioL1elz7ri7ImYAADY7e3B _-c694.png "/>

concept and function of flow

A stream is a set of sequences of bytes that have a starting point and an end point, a generic or abstract of the data transfer. That is, the transmission of data between the two devices is called the flow, the essence of the flow is data transmission, according to the data transmission characteristics of the stream abstracted into various classes, convenient and more intuitive data manipulation.

Classification of IO streams
    • Divided into: character stream and byte stream according to different types of processing data

    • Divided into: input stream and output stream according to data flow

Character Stream and Byte stream

The origin of a character stream: Because of the difference in data encoding, there is a stream object that efficiently operates on characters. The essence is actually based on the byte stream reading, to check the specified code table. The difference between a byte stream and a character stream:

    • Read-write units are different: byte stream in bytes (8bit), character stream in characters, according to the Code table mapping characters, one can read multiple bytes at a time.

    • The processing object is different: The byte stream can handle all types of data (slices, AVI, etc.), while the character stream can only handle data of the type of characters.

Conclusion: As long as you are working with plain text data, you will prefer to use character streams. In addition, byte streams are used.

Input stream and output stream

The input stream can only be read, the output stream can only write operations, the program needs to be transmitted according to the different characteristics of the data to use different streams. (both relative to the program itself)

Java io Stream objects

1. Input byte stream InputStream The inheritance graph of input byte stream in IO can be seen as follows:

    1. InputStream is the parent class for all input byte streams, which is an abstract class.

    2. Bytearrayinputstream, StringBufferInputStream, FileInputStream are three basic media streams that read data from byte arrays, StringBuffer, and local files, respectively. PipedInputStream is to read data from a pipeline shared with other threads, followed by a separate introduction to piped-related knowledge.

    3. ObjectInputStream and all FilterInputStream subclasses are decorative streams (the protagonist of the adorner pattern).

2. Output byte stream outputstream

The inheritance graph of output byte stream in IO can be seen as follows:

    1. OutputStream is the parent class for all output byte streams, which is an abstract class.

    2. Bytearrayoutputstream, FileOutputStream, are two basic media streams that write data to byte arrays, and local files, respectively. PipedOutputStream is writing data to a pipeline that is shared with other threads.

    3. ObjectOutputStream and all filteroutputstream subclasses are decorative streams.

3. Input and output of the byte stream

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/85/80/wkiol1elz9dtxoaoaajjmavtmlk886.png "title=" 3.png "alt=" Wkiol1elz9dtxoaoaajjmavtmlk886.png "/>

The blue is the main counterpart, and the red part is the non-corresponding part. The dotted purple section represents these streams that are typically used with . As you can see from the above figure, the byte stream in Java IO is extremely symmetrical. "Existence and reasonableness" Let's look at some of the less symmetrical classes in these byte streams!

    1. Linenumberinputstream The main completion of reading data from the stream, you will get the corresponding line number, as to when the branch, where the branch is determined by the modification of the active, not in the original has such a line number. There is no corresponding section in the output section, we can create a linenumberoutputstream ourselves, the initial write will have a baseline line number, each time you encounter a newline in the next row will be added a line number, it seems to be possible. It seems to be a less inflow.

    2. The function of Pushbackinputstream is to look at the last byte, and put it in a buffer if not satisfied. Mainly used in the compiler syntax, lexical analysis part. The output portion of the Bufferedoutputstream almost achieves similar functionality.

    3. StringBufferInputStream has been deprecated, itself should not appear in the InputStream part, mainly because the string should belong to the range of character stream. Has been abandoned, of course, the output part of the need for it is not necessary! It is also allowed to exist just to keep the version backwards compatible.

    4. Sequenceinputstream can be thought of as a tool class that reads two or more input streams as an input stream sequentially. Completely can be removed from the IO package, but also does not affect the structure of the IO package, but let it more "pure"-pure decorator mode.

    5. PrintStream can also be considered as an auxiliary tool. It is possible to write data to other output streams, or to fileinputstream, with internal implementations or buffers. is essentially a tool for the integrated use of other flows. You can kick out the IO package as well! System.out and System.out are examples of PrintStream!

4. Character input stream reader

As you can see in the inheritance diagram above:

    1. Reader is the parent class of all the input character streams, which is an abstract class.

    2. Charreader, StringReader, are two basic media streams that read data from a char array, a string, respectively. Pipedreader is to read data from a pipeline that is shared with other threads.

    3. BufferedReader is obviously an adorner, and its subclasses are responsible for decorating other reader objects.

    4. FilterReader is the parent of all custom-specific decorative streams whose subclasses pushbackreader the reader object, adding a line number.

    5. InputStreamReader is a bridge between the byte stream and the character stream, which transforms the stream of bytes into character streams. FileReader can be said to be a common tool class to achieve this function, and in its source code it is obvious to use the method of transforming FileInputStream into reader. We can get some tricks out of this class. The purpose and usage of each class in Reader is basically consistent with the class used in InputStream. There will be a correspondence between reader and InputStream in the back.

5. Character output stream writer

As you can see in the diagram above:

    1. Writer is the parent of all output character streams, which is an abstract class.

    2. Chararraywriter, StringWriter, are two basic media streams that write data to a char array, a String, respectively. PipedWriter is writing data to a pipeline that is shared with other threads.

    3. BufferedWriter is an adorner that provides buffering functionality for writers.

    4. PrintWriter and PrintStream are extremely similar, and function and use are very similar.

    5. OutputStreamWriter is a bridge from OutputStream to writer, and its subclass filewriter is actually a concrete class to implement this function (specifically, one sourcecode). Functionality and use are very similar to OutputStream, with their corresponding graphs behind them.

6.correspondence of input and output of character stream

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/85/80/wkiom1elz9ua87zmaait6-g6sii274.png "title=" 4.png "alt=" Wkiom1elz9ua87zmaait6-g6sii274.png "/>

7. Character stream and byte stream conversion

Features of the conversion stream:

    1. It's a bridge between a character stream and a byte stream.

    2. Converts the read byte data to a character by a specified encoding

    3. Converts the specified encoding of the read character data into bytes

When do I use a transform stream?

    1. When there is a conversion action between the byte and the character;

    2. The data for the stream operation needs to be encoded or decoded.

The specific object embodies:

    1. InputStreamReader: byte-to-character Bridge

    2. OutputStreamWriter: character-to-byte bridge

These two stream objects are members of the character system, they have transformations, and themselves are character streams, so it is necessary to pass in a byte stream object when constructing.

8.File class

The file class is an object that encapsulates files and folders in the file system, and can manipulate files and folders through the idea of an object. The file class holds various meta-data information for files or directories, including file name, file length, last modified time, whether it is readable, gets the path name of the current file, determines whether the specified file exists, obtains a list of files in the current directory, and creates and deletes files and directories.

9.RandomAccessFile class

This object is not a member of the flow system, it encapsulates a byte stream, and also encapsulates a buffer (a character array), manipulating the data in a character array with an internal pointer. The object features:

    1. The object can only manipulate files, so the constructor receives two types of parameters: A. string file path; B.file object.

    2. The object can read and write to the file, and can specify the mode of operation when the object is instantiated (R,RW)

Note: When the object is instantiated, it is created automatically if the file to be manipulated does not exist, and if the file exists, the write data does not specify a location and is written from scratch, overwriting the original content. can be used for multithreaded downloads or for multiple threads to write data to a file at the same time.


Java IO Stream Learning Summary

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.