Character input and output stream with cache

Source: Internet
Author: User

Caching can be said to be a performance optimization for I/O. The cache stream adds a memory buffer to the I/O stream. With buffers, it is possible to execute the skip () \mark () and Reset () methods on the stream.

Outline:

BufferedReader class and Bufferwriter class

The BufferedReader class and the Bufferwriter class inherit the reader class and the writer class respectively. These two classes also have an internal caching mechanism, and can be input and output in the unit behavior units.

According to the characteristics of the BufferedReader class, the process of reading a file from character data can be summed up: character data = = "buffereawriter==" outoutstreamwriter== "outoutstream==" file.

The common methods of the BufferedReader class are as follows:

Read () Method: reads a single character.

ReadLine () Method: reads a line of text and returns it as a string, or null if no data is readable.

Write (string s,int off,int len) method: Writes a portion of a string.

Flush () Method: Refreshes the cache of the process.

NewLine () Method: writes a row separator.

When using the Write () method of the BufferedWriter class, the data is not written directly into the output stream, but first into the buffer, if you want to immediately write the buffer data to the output stream, be sure to call the Flush () method.

ImportJava.io.*; Public classTestFile4 { Public Static voidMain (string[] args) {Try{File F=NewFile ("D:\\test.txt"); //with Cache//the implementation class of writer interfaceFileWriter fw=NewFileWriter (F,true); //Cache write classes that need to be passed into writer instances when constructedBufferedWriter bw=NewBufferedWriter (FW); Bw.write ("\ n This is the cached way to write the string"); //automatically manage caching//Auto Write: 1. Cache is full 2. Before the cache is closedBw.flush ();//actively emptying the cache, writing dataBw.write ("\ n This is the string that was written after the cache was emptied.");            Bw.close ();                        Fw.close (); System.out.println ("Write Complete"); //Cache ReadFileReader FR=NewFileReader (f); BufferedReader BR=NewBufferedReader (FR); //first time Read//String str=br.readline ();//            //System.out.println (str);//            //While (Str!=null)//            {//System.out.println (str);//                //str=br.readline ();//            }                         while(true) {String str=Br.readline (); if(str==NULL)                {                     Break;            } System.out.println (str); }                                                        }        Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); }                    }}
View Code

Character input and output stream with cache

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.