Java IO Stream Learning

Source: Internet
Author: User
Tags sca

In recent days carefully learned Java IO stream, originally intended to watch video through the video to learn, but later found that the video does not understand and feel not very easy to use, so through the Baidu and API documents to learn Java IO stream

The IO stream can have two classifications, one is a byte stream and a character stream, and the other is the input stream and the output stream.

First talk about the word stream and character stream: First of all, in fact, the byte stream is not used in the operation of the buffer, the file itself is directly manipulated, but the character stream at the time of operation will be used in the buffer, is through the buffer to manipulate the file. The other difference between the byte stream and the character stream is that the reading and writing units are different, no doubt, the character stream is in characters, the byte stream is in bytes (a byte 8 bit), so that the two processing objects are different, the byte stream can handle all types of data, and the character stream can only handle the data type. So we can get a conclusion that as long as we are dealing with plain text data, we prefer to use character streams, in addition to byte streams.

Then we'll talk about the input stream and the output stream. To be honest, from the time I started learning C, I entered and exported, and output and input were confusing and not clear. It takes a lot of contact to keep the difference in mind. Input generally refers to the external files (such as hard disk, keyboard, etc.) to obtain data, and output refers to the output from the program itself to the external data (such as writing to the file, display on the screen). There is another difference between the input stream and the output stream, which is that the input stream can only be read, and the output stream can only be written.

With these two big categories, let's look at some of the more detailed sections and see a class diagram.

As seen by the inheritance graph, the character input stream reader is the parent of all the character input streams. The Pipedreader is primarily capable of reading data through pipelines Cheng Jiancheng with other wires.

The InputStreamReader is a bridge between the byte stream and the character stream, which can be transformed into a character stream.

The character output stream writer is the parent class for all character output streams. Other subclasses correspond to each subclass of reader.

The input byte stream InputStream is the parent class for all input stream streams. Bytearrayinputstream, StringBufferInputStream, FileInputStream are read into the data from the corresponding file respectively

Instead, PipedInputStream reads data from pipelines that are shared with other threads.

The output byte stream Outputsteam is the parent class for all output stream streams. Other subclasses correspond to each other in Inputsteam.

In addition to the byte stream and character stream, the entire IO class also includes the bytes and characters conversion streams.

Outputstreramwriter the output character into a byte stream

InputStreamReader converting the input byte stream to a character stream

stream byte output to character output flow writer out= new OutputStreamWriter( new FileOutputStream(file)); stream byte input to character input streams reader read= new InputStreamReader( new FileInputStream(file));

There is a problem, God demon time to use the conversion flow: When the byte and the character between the conversion action and the flow operation of the data need to encode or decode the time.

enter destination with content as output, use memory operation FlowBytearrayinputstream primarily writes content to content Bytearrayoutputstream primarily exports content from memorycontent operation flow is generally used to generate some temporary information to use, so as to avoid the trouble of deletion

The merge stream Sequenceinputstream Sequenceinputstream is primarily used to merge 2 streams together, such as merging the contents of two txt into another txt.

PushbackinputstreamFallback Flow: You can return some of the data read in to the data buffer

BufferedReader You can only accept buffers for character streams, because each text needs to occupy two bytes, so you need to system.in This byte input rheology is a character input stream, using:

BufferedReader buf =newBufferedReader(                newInputStreamReader(System.in));
More commonly used is the use ofScannerclass for data entryScanner SCA = new Scanner(System.in);          // 读一个整数          int temp = sca.nextInt(); 查了下api文档关于scanner的内容发现Scanner的参数代表的是数据从哪里扫描,scanner可以接受任何的输入流 System.out.println () redirect output (can change to console output to write file operation)System.err Redirect (error information can be written to file save)

In the flow operation often need to use: and \ Not recommended directly, it is best to call two constants File.separator (\) and file.pathseparator (:), so that the cross-platform operation will be more compatible

if you want to wrap the file, you need to use the\ r \ n"

Java IO Stream Learning

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.