Java IO (input output) stream & lt; 2 & gt;

Source: Internet
Author: User

Java IO (input output) stream <2>
1. The buffer of the upstream stream 1. What is the buffer, also known as the cache, is a part of the memory space. That is to say, a certain amount of storage space is reserved in the memory space, which is used to buffer input or output data. This reserved space is called a buffer zone. The buffer zone is divided into the input buffer zone and the output buffer zone based on whether it is an input device or an output device. 2. Why should we introduce a buffer zone as a memory zone, which is used between the input and output devices and the CPU to cache data. It is to reduce system calls and make them work efficiently. 3. Stream buffering 3.

1 BufferWriter instance 2 3 // use the BufferedWriter buffer object to read data 4 public class BufferedWriterDemo {5 6 public static void main (String [] args) throws IOException {7 // create a file stream to store data and pass it to BufferedWriter object 8 FileWriter fw = new FileWriter ("BuffWriter_copy.txt "); 9 // create a buffer object and pass the file stream object to its constructor 10 BufferedWriter bufw = new BufferedWriter (fw ); 11 // write data to the buffer 12 for (int I = 0; I <10; I ++) {13 14 bufw. write ("abcde" + I); 15 16 bufw. newLine (); 17 // refresh the data in the memory to 18 bufw in the file. flush (); 19} 20 // disable buffer 21 bufw. close (); 22} 23 24}

 

1 import java. io. *; running result 3.2 BufferedReader instance
Import java. io. *;/** create a BufferedReader buffer and use the newLine and readLine Methods */public class BUfferedReaderDemo {public static void main (String [] args) throws IOException {// create a file stream, pass the data to the buffer constructor FileReader fr = new FileReader ("test.txt"); // create a buffer and call its constructor BufferedReader bufr = new BufferedReader (fr ); // print the data. // the final data returned by BufferedReader is String. If so, it is null String line = null. // call the readLine method of BufferedReader while (lin E = bufr. readLine ())! = Null) {System. out. println (line );}}}

 

Result 2: the Integrated instance copies the content of a file to another file using the character buffer.
Import java. io. *;/** use the character buffer to copy the content of a file to another file */public class BufferWriterDemo {public static void main (String [] args) {// create a buffer write object and initialize it to null BufferedWriter bufw = null; // create a buffer read object and initialize it to null BufferedReader bufr = null; try {// The buffer constructor must have a parameter // create a Write File stream and pass it to the constructor bufw = new BufferedWriter (new FileWriter ("buffer_copy.txt" )); // create a read File stream and pass it to the constructor bufr = new BufferedReader (new FileReader ("File Read. java "); // The buffer returns a String value. When no data in the buffer zone is null String line = null; // The buffer write object has a readLine function, used to read data by row while (line = bufr. readLine ())! = Null) {// The buffer write object also has the write function bufw. write (line); // newLine will wrap the data read, that is, store the bufw according to the original data format. newLine (); // refresh the data to the bufw in the file. flush () ;}} catch (IOException e) {throw new RuntimeException ("read/write error");} finally {try {if (bufw! = Null) {bufw. close () ;}catch (IOException e) {throw new RuntimeException ("write error") ;}try {if (bufr! = Null) {bufr. close () ;}} catch (IOException e) {throw new RuntimeException ("read error ");}}}}

 


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.