Read/write buffers for IO character streams (bufferedwriter, BufferedReader)

Source: Internet
Author: User

Buffers are used to improve efficiency, and you must explicitly stream objects before you can use buffers. Just like taking a cup of water, you have to know where to put the cup.

Bufferedwriterdemo (input character stream buffer demo):

ImportJava.io.*;/*The presence of buffers occurs in order to improve the operational efficiency of the flow. So before you create a buffer, you must have a Stream object first. A cross-platform line break is provided in the buffer object. NewLine ();*/classbufferedwriterdemo{ Public Static voidMain (string[] args) {//defines a stream object. FileWriter FW =NULL; //in order to improve the character write stream efficiency. Added buffering technology. BufferedWriter BUFW =NULL; Try{FW=NewFileWriter ("E:\\bufferedwriterdemo.txt"); BUFW=NewBufferedWriter (FW);//simply pass the stream object that needs to be improved as an argument to the constructor of the buffer. Bufw.write ("FSAFSFASFD"); }        Catch(IOException e) {System.out.println (e.tostring ()); }        finally        {            Try            {                if(fw!=NULL) {bufw.close (); }            }            Catch(IOException e) {} }}}
Bufferwriterdemo

Bufferedreaderdemo (read character stream buffer demo):

/*character Read Stream buffer: This buffer provides a way to read one line at a time readLine, which facilitates the acquisition of text data. When NULL is returned, the expression reads to the end of the file. When the ReadLine method returns, only the data content before the carriage return is returned. Does not return carriage returns. */ImportJava.io.*;classBufferedreaderdemo { Public Static voidMain (string[] args) {//Create a character read stream object firstFileReader FR =NULL; //The character reads the stream object buffer. BufferedReader BUFR =NULL; Try{FR=NewFileReader ("E:\\bufferedwriterdemo.txt"); BUFR=NewBufferedReader (FR);//in order to improve efficiency. Added buffer technology. The constructor that passes the character read stream object as a parameter to the buffered object. String result =NULL;  while((Result=bufr.readline ())! =NULL) {System.out.println (result); }        }        Catch(IOException e) {System.out.println (e.tostring ()); }        finally        {            Try            {                if(fr!=NULL) {bufr.close ();//reading a character stream buffer object can close the resource directly because it inherits the closed resource method of the object reader                }            }            Catch(IOException e) {} }}}
Bufferedreaderdemo

To copy a file using the cache demo:

ImportJava.io.*;classuserbufferedcopyfiledemo{ Public Static voidMain (string[] args) {//create read and write file character stream objectsFileReader FR =NULL; FileWriter FW=NULL; //create a buffer for the read-write character stream objectBufferedReader BUFR =NULL; BufferedWriter BUFW=NULL; //defines the character array and counter used to hold the Read data information//Char [] array = new char[1024];//int len = 0;        Try{FR=NewFileReader ("E:\\bufferedwriterdemo.txt"); FW=NewFileWriter ("E:\\userbuffercopyfiledemo.txt"); BUFR=NewBufferedReader (FR); BUFW=NewBufferedWriter (FW); //how to read into an array//While ((Len=bufr.read (array))!=-1)//            {//Bufw.write (Array,0,len);//            }            //A row of rows readsString line =NULL;  while((Line=bufr.readline ())! =NULL) {bufw.write (line); Bufw.newline ();//outputs a newline character. Bufw.flush ();//to avoid situations such as power outages, refresh the stream every time you finish writing a line.             }        }        Catch(IOException e) {System.out.println (e.tostring ()); }        finally        {            Try            {                if(fr!=NULL) {bufr.close (); }            }            Catch(IOException e) {}Try            {                if(fw!=NULL) {bufw.close (); }            }            Catch(IOException e) {} }}}
Userbufferedcopyfiledemo

Read/write buffers for IO character streams (bufferedwriter, BufferedReader)

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.