Java IO (Java input and output stream) detailed

Source: Internet
Author: User

1, read/write (input/output) is for me, not for you to manipulate the object, such as reading the file is I read the file, for me is input, and I write files, for me is the output.
2, Java IO uses the packaging mode, a flow wrapper another stream, to achieve better purposes. What is the rule of a flow wrapper for another flow? Use the byte stream to read the file, FileInputStream (new file ()), which you can use Bufferedinputstream (new FileInputStream ()) to wrap it
As for why Bufferedinputstream can be packaged fileinputstream. The Bufferedinputstream constructor Bufferedinputstream (InputStream in), and FileInputStream's parent class is InputStream, all of it.
3. Byte stream conversion to character stream: Inputstreamreader/outputstreamwriter
Note: When using FileReader to manipulate text data, the object uses the default encoding table, which is FileReader fr=new filereader ("A.txt");   With InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt")); Have the same meaning.
If you want to encode a table using the specified table, you must use a transform stream, that is, if the character data in the file in A.txt is encoded by UTF-8, then the encoding table must be specified when reading, so it is necessary to convert the stream.
namely: InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt"), utf-8);




Classes or interfaces related to Java stream operations:





Java Flow class diagram structure:










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
? According to different types of processing data: character stream and Byte stream
? According to the data flow is divided into: input stream and output stream

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.), whereas a 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.






Java IO Stream objects
1. Input byte stream Inputstreamio The inheritance graph of the input bytes is visible, you can see:


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 to 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






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 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.
The function of 2.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 section, 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 stream of characters, 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 class of all the 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 outputstream to writer Conversion Bridge, its sub-class filewriter is actually a specific class to implement this function (specifically, can study a sourcecode). Functionality and use are very similar to OutputStream, with their corresponding graphs behind them.

6. Correspondence of input and output of character stream






7. Character stream and byte stream conversion
Features of the conversion stream:


1. It is a bridge between a character stream and a byte stream
2. Can convert the read byte data to a character by a specified encoding
3. Can convert the read character data to byte by the specified encoding
When do I use a transform stream?


1. When there is a conversion action between the byte and the character;
2. When data for streaming operations 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 operation mode 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 (Java input and output stream) detailed

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.