02JavaIO detailed _iol three categories of flow and InputStream and OutputStream

Source: Internet
Author: User

The IO stream refers to the input stream and the output stream. How to define the input and output stream, to the program as a reference, flow into the program is the input stream, from the program inside Out is the output stream.

Introduction to Input/output streams:

There are three categories of streams:

1. Functionally divided into: input stream and output stream.

2. The stream structure is divided into: byte stream and character stream.

3. Node stream and filter stream.

Note: The above mentions the byte stream and the character stream. In fact, the character stream is based on the word stream, we manipulate the string more convenient ah, so there is a character stream. After the word stream is mastered, the character stream is very simple.

Description of character stream and byte stream:

The basis of the byte stream is InputStream and outputstream. We are not new InputStream and outputstream. They are all abstract classes, and we actually use their subclasses to implement them.

and the character stream is based on reader and writer. They are also abstract classes.

Description of output stream and input stream

Note: Both InputStream and reader are input streams, which are entered into the program from the outside.

Both OutputStream and writer are output streams, which flow from the program to the outside.

Read and write logic for the input stream and output stream:

The logic of the input stream's read data:

1. Open the stream.

2.1.1-point reading, use while statement to judge, is not there.

3. Continue reading if you have some words.

4. Close the stream when you are done reading it.

The logic of the output stream's write data:

1. Open the stream.

2.1.1 points to write, use while statement to judge, is not there.

3. Keep writing if you have some words.

4. Close the stream when you're done.

Description of node stream and filter stream

Node stream: A stream class that reads and writes from a specific place. For example, a disk or a piece of memory area.

Filter stream: Use node stream as input or output. The filtered stream is created using an existing input stream or an output stream connection.

Simply put: the node flow is directly interacting with the target. The filter flow is to do some packing to the node flow and deal with the node flow.

Here's a diagram that shows what a node stream is, what is a filtered stream: (aspect of the input stream)

is the node stream that deals directly with the TXT document. The two circles after that are the filter flow. Other words. Filtering flow is the flow of the node to packaging, so that it has more functions. And finally flowed into our Java program.

We've covered three categories and we'll explain what we're going to do next:

Let's talk about the byte stream first:

There is an abstract class called InputStream in the byte stream:

So why is it defined as an abstract class?

Let's look at the method inside the InputStream:

It is obvious that there is an abstract int read () such an abstraction. So you define it as an abstract class.

Let's think about why the first read method in the above diagram is abstract, and the remaining two read methods are specific.

Answer: because two specific read methods are implemented by invoking that abstract method . Obviously, many subclasses will inherit the InputStream abstract class and then implement the abstract Read method. Then we call the specific read method is actually used is that quilt class overwrite that the abstract read method. (How perfect).

To explain to sketch:

It's perfect. In this case, InputStream has a lot of subclasses, there are processing byte arrays, there is processing the network, processing the file. These subclasses can have their own read method. This abstract parent class provides an abstract version of the good.

There is no need to provide a specific version, even if a specific version is not meaningful.

Here's a case study that shows how to read a file. Java provides us with FileInputStream This implementation inutstream the subclass of the parent class to handle the read of the file.

The read () method inside the FileInputStream is designed to manipulate files.

Let's take a look at the source code of read () in FileInputStream

Obviously this is native, is written in C, we can not see.

The following is the most standard wording, is the routine, remember.

 PackageCom.guigu.shen.File;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classFiletest { Public Static voidMain (string[] args)throwsIOException {inputstream FileInputStream=NewFileInputStream ("D:/abc/xyz/hello/hello.txt");//creates a byte array. Every time I read the message in here ,byte[] buffer=New byte[200];//The actual size of bytes per read;intLenght=0;/** Lenght=fileinputstream.read (buffer,0,200) refers to reading data from the "D:abc/xyz/hello/hello.txt" file. Each time the data is read into the buffer, the buffer array holds the starting position from 0 to 200, that is, I read 200 bytes each time. It is then stored inside the buffer array. The location of the buffer is 0 to buffer * of 200. The size of the buffer defined is 200, which means that I have only 200 of the data I read at most, or the buffer does not fit. So the maximum can only write 0,200; * Then assign the length of the true byte array that I read each time; * Assume the following two cases 1.d:abc/xyz/hello/hello.txt only 150 bytes in size. So I read it all at once. The size of length is 150, and then it is return-1. * 2. Assume that the size of the D:abc/xyz/hello/hello.txt is 450 bytes in size. The length of the first read return is 200. The second return length is also 200 * The third return length is only 50. The length of the fourth return is-1: * * The length here is always less than or equal to the sum of the contents of read (buffer,0,200)  **/ while( -1!= (Lenght=fileinputstream.read (buffer,0,200))){    //converts every byte that is stored in buffer into a string. String str=NewString (buffer, 0, lenght); System.out.println (str);} Fileinputstream.close ();;}}

This writing is the most common, must be closed eyes can be written out. And the importance of collections is the same.

The next step is to introduce the outputstream of the byte stream, and InputStream is just the opposite.

 PackageCom.guigu.shen.File;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classFiletest { Public Static voidMain (string[] args)throwsIOException {fileoutputstream FileOutputStream=NewFileOutputStream ("D:/abc/xyz/hello/hello.txt"); String Str= "Helloword";//Turn a string into a byte arraybyte[] buffer=str.getbytes ();//writes the contents of a byte array to a folderfileoutputstream.write (buffer); Fileoutputstream.close ();}}

02JavaIO detailed _iol three categories of flow and InputStream and OutputStream

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.