Java IO (Input output) stream < two >

Source: Internet
Author: User

I. Buffer of character streams

1. What is a buffer
A buffer is also known as a cache, which is part of the memory space. In other words, a certain amount of storage space is reserved in the memory space, which is used to buffer the input or output data,

This part of the reserved space is called a buffer. The buffer is divided into input buffers and output buffers depending on whether it corresponds to an input device or an output device.

2. Why to introduce buffers

A buffer is an area of memory that is used between input and the CPU to cache data. It enables low-speed input and high-speed CPUs to work in coordination to avoid low

The input consumes the CPU and frees up the CPU, enabling it to work efficiently.

3. Buffering of character streams

3.1 Bufferwriter Instances

1 ImportJava.io.*;2 3 //using the BufferedWriter buffer object to read data4  Public classBufferedwriterdemo {5 6      Public Static voidMain (string[] args)throwsioexception{7         //Create a new file stream to store the data and pass it to the BufferedWriter object8FileWriter FW =NewFileWriter ("Buffwriter_copy.txt");9         //creates a new buffer object and passes the file stream object to its constructorTenBufferedWriter bufw=NewBufferedWriter (FW); One         //write data to the buffer A          for(inti=0;i<10;i++){ -              -Bufw.write ("ABCDE" +i); the           - bufw.newline (); -          //flush in-memory data to a file - Bufw.flush (); +         } -         //Close Buffer + bufw.close (); A     } at  -}

Run results

3.2 BufferedReader Instances

ImportJava.io.*;/** Establish BufferedReader buffers, using newline and ReadLine methods*/ Public classBufferedreaderdemo { Public Static voidMain (string[] args)throwsIOException {//creates a new file stream and passes the data to the buffer constructorFileReader FR =NewFileReader ("Test.txt") ; //creates a new buffer, calling its constructorBufferedReader BUFR =NewBufferedReader (FR); //Print the Data//because the last data returned by BufferedReader is string, null if it is lastString line=NULL; //call the ReadLine method of BufferedReader         while((Line=bufr.readline ())! =NULL) {System.out.println (line); }            }}

Run results

Two. General examples

use character buffers to copy one file content to another file

ImportJava.io.*;/** Use character buffers to copy one file content to another file*/ Public classBufferwriterdemo { Public Static voidMain (string[] args) {//creates a buffer write object and initializes it to nullBufferedWriter bufw=NULL; //creates a buffer read object and initializes it to nullBufferedReader bufr=NULL; Try{            //the constructor for the buffer must have a parameter//creates a new stream of write files and passes them to the constructor of the buffer write objectbufw=NewBufferedWriter (NewFileWriter ("Buffer_copy.txt")); //creates a new stream of read files and passes it to the constructor of the buffer read ObjectBufr=NewBufferedReader (NewFileReader ("Fileread.java")); //The buffer returns a string value when no data in the buffer is a value of NULLString line=NULL; //the buffer write object has a ReadLine function to read data by row             while((Line=bufr.readline ())! =NULL){            //Buffer Write object also has write functionBufw.write (line); //NewLine will read the data wrap, that is, the original data format consistent to storeBufw.newline (); //refresh the data into the fileBufw.flush (); }        }                Catch(IOException e) {Throw NewRuntimeException ("Read and write Errors"); }        finally{                         Try{                                if(bufw!=NULL) {bufw.close (); }                      }            Catch(IOException e) {Throw NewRuntimeException ("Write Error"); }             Try{                                  if(bufr!=NULL) {bufr.close (); }             }             Catch(IOException e) {Throw NewRuntimeException ("Read error"); }        }    }}

Run results

Java IO (Input output) stream < two >

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.