Java Fundamentals Hardening IO Stream Note 37: Bufferedwriter/bufferedreader use of character stream buffer stream

Source: Internet
Author: User

1. Character Stream Buffer stream:

Character stream for efficient reading and writing, also provides the corresponding characters buffer stream.

BufferedWriter: Character buffered output stream

BufferedReader: Character buffered input stream

2. BufferedWriter use:

Writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings. You can specify the size of the buffer, or accept the default size. In most cases, the default value is large enough.

(1)the construction method of BufferedWriter:

Construction Method Summary
BufferedWriter(Writer out)
Creates a buffered character output stream that uses the default size output buffer.
BufferedWriter(Writer out, int sz)
Creates a new buffered character output stream that uses a given size output buffer.

(2) Function methodof BufferedWriter :

1 Method Summary2  voidClose ()3 close the stream, but refresh it first. 4  voidFlush ()5 refreshes the buffer of the stream. 6  voidNewLine ()7 writes a line delimiter. 8  voidWriteChar[] Cbuf,intOffintlen)9 writes a portion of a character array. Ten  voidWriteintc) One writes a single character.  A  voidWrite (String s,intOffintlen) -Writes a portion of a string.

(3)bufferedwriter Use code example:

1  Packagecn.itcast_05;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.FileWriter;5 Importjava.io.IOException;6 7 /*8 * BufferedWriter: character buffered output stream 9 * writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings. Ten * You can specify the size of the buffer, or accept the default size. In most cases, the default value is large enough.  One  */ A  Public classBufferedwriterdemo { -      Public Static voidMain (string[] args)throwsIOException { -         //BufferedWriter (Writer out) the         //bufferedwriter bw = new BufferedWriter (New OutputStreamWriter ( -         //new FileOutputStream ("Bw.txt" )); -  -BufferedWriter BW =NewBufferedWriter (NewFileWriter ("Bw.txt")); +  -Bw.write ("Hello"); +Bw.write ("World"); ABw.write ("Java"); at Bw.flush (); -  - bw.close (); -     } -}

Run the effect as follows:

3. BufferedReader use:

reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows. You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.

(1)the construction method of BufferedReader:

Construction Method Summary
BufferedReader(Reader in)
Creates a buffered character input stream that uses the default size input buffer.
BufferedReader(Reader in, int sz)
Creates a buffered character input stream that uses the specified size input buffer.

(2) Function method of BufferedReader :

1 Method Summary2  voidClose ()3 closes the stream and frees all resources associated with it. 4  voidMarkintreadaheadlimit)5 marks the current position in the stream. 6  Booleanmarksupported ()7 determines whether this stream supports the mark () operation (it must support). 8  intRead ()9 reads a single character. Ten  intReadChar[] Cbuf,intOffintlen) One reads a character into a portion of an array.  A String readLine () - reads a line of text.  -  BooleanReady () the determines whether this stream is ready to be read.  -  voidReset () - Resets the stream to the latest markup.  -  LongSkipLongN) +Skips a character.

(3)BufferedReader Use code example:

1  Packagecn.itcast_05;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.FileReader;5 Importjava.io.IOException;6 7 /*8 * BufferedReader9 * Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows. Ten * You can specify the size of the buffer or you can use the default size. In most cases, the default value is large enough.  One  *  A * BufferedReader (Reader in)  -  */ -  Public classBufferedreaderdemo { the      Public Static voidMain (string[] args)throwsIOException { -         //creating a character buffer input stream object -BufferedReader br =NewBufferedReader (NewFileReader ("Bw.txt")); -  +         //Mode 1: read one character at a time  -         //int ch = 0; +         //While ((ch = br.read ())! =-1) { A         //System.out.print ((char) ch); at         // } -  -         //Mode 2: read one character array at a time  -         Char[] CHS =New Char[1024]; -         intLen = 0; -          while(len = Br.read (CHS))! =-1) { inSystem.out.print (NewString (CHS, 0, Len)); -         } to  +         //Freeing Resources - br.close (); the     } *}

Run the effect as follows:

Java Fundamentals Hardening IO Stream Note 37: Bufferedwriter/bufferedreader use of character stream buffer stream

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.