Understanding of Java file IO streams (2)

Source: Internet
Author: User

1. The buffer of the upstream stream

(1) The emergence of the buffer zone is to improve the data read and write efficiency.

(2) Corresponding classes: bufferedwriter and bufferedreader

(3) the buffer zone can be used only when combined with a stream.

/*** Shard stream read buffer: * this buffer provides the Readline Method for reading a row at a time. If the return value is null, it indicates that it is read to the end. It is convenient to obtain text data. */Package COM. heima; import Java. io. *; public class bufferedreaderdemo {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // create a stream reading object and file associated with filereader Fr = new filereader ("buf.txt"); // to improve efficiency, add the buffer technology. Pass the character reading Stream object as a parameter to the constructor of the slow object. Bufferedreader BR = new bufferedreader (FR); string line = NULL; while (line = Br. Readline ())! = NULL) {system. Out. println (line) ;}br. Close ();}}
/** Shard stream write buffer */package COM. heima; import Java. io. *; public class bufferwriterdemo {public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stub // create a character to write to the stream object. Filewriter fw = new filewriter ("buf.txt"); // Buffer technology is added to improve character writing efficiency. // You only need to pass the stream object that needs to improve efficiency as a parameter to the constructor of the buffer zone. Bufferedwriter BW = new bufferedwriter (FW); For (INT x = 1; x <5; X ++) {BW. write ("ABCD"); BW. newline (); BW. flush ();} // remember to refresh as long as the buffer zone is used. // BW. Flush (); // actually close the buffer, which is the stream object in the buffer. Bw. Close ();}}

/** Copy A. Java file through a buffer */

package com.heima;import java.io.*;
public class CopyTextByBuf {
Public static void main (string [] ARGs) {// todo auto-generated method stub bufferedreader bufr = NULL; bufferedwriter bufw = NULL; try {bufr = new bufferedreader (New filereader ("bufferedwriterdemo. java "); bufw = new bufferedwriter (New filewriter (" bufwriter_copy.txt "); string line = NULL; while (line = bufr. readline ())! = NULL) // The Readline method returns the data content before the carriage return. Carriage return is not returned. {Bufw. write (line); bufw. newline (); bufw. flush () ;}} catch (ioexception e) {Throw new runtimeexception ("read/write failed");} finally {try {If (bufr! = NULL) bufr. close ();} catch (ioexception e) {// todo auto-generated catch blockthrow new runtimeexception ("read close failed");} Try {If (bufw! = NULL) bufw. Close ();} catch (ioexception e) {// todo auto-generated catch blockthrow new runtimeexception ("Write close failed ");}}}}

Note: Principle of the Readline method: no matter how many characters a row reads. In fact, they are all read one by one on the hard disk. Therefore, we finally use the read method to read one at a time.

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.