The following is a description of the Java.io.BufferedWriter inheritance relationship for the Java SE 7 API.
BufferedWriter can write text to a character stream. It caches characters in order to improve the efficiency of writing characters.
The size of the buffer must be clear, otherwise the default size will be used. The default size is large enough for most cases.
BufferedWriter provides a method of newline () to be used for line wrapping. After all, not all platforms use the ' \ n ' line-wrapping method.
A Writer object writes the output immediately to the current stream of characters or to a byte stream.
Generally speaking, if this write is not very urgent, it is recommended to use the BufferedWriter object to wrap a time-consuming writer object (for example, Filewriters,outputstreamwriters).
Specific examples are as follows:
PrintWriter out = new PrintWriter (new Bufferwriter (New FileWriter ("Foo.out"));
In this example, the output of the PrintWriter object is cached to a file. If there is no cache, each call to the print () method will say that the characters to be output are immediately converted to bytes, so that the write is very inefficient.
————————————————————————————————————————————————————————————————————
The API has already explained that bufferedwriter can improve the efficiency of writing a character stream. For time-consuming writes, using the Bufferwriter object is a very sensible choice.
Below is the source code of BufferedWriter.
Packagejava.io; Public classBufferedWriterextendsWriter {PrivateWriter out; Private Charcb[]; Private intNchars, Nextchar; Private Static intDefaultcharbuffersize = 8192; PrivateString LineSeparator; PublicBufferedWriter (Writer out) { This(out, defaultcharbuffersize); } PublicBufferedWriter (Writer out,intSZ) { Super(out); if(SZ <= 0) Throw NewIllegalArgumentException ("Buffer size <= 0"); This. Out =Out ; CB=New Char[SZ]; Nchars=sz; Nextchar= 0; LineSeparator=java.security.AccessController.doPrivileged (NewSun.security.action.GetPropertyAction ("Line.separator")); } Private voidEnsureopen ()throwsIOException {if(out = =NULL) Throw NewIOException ("Stream closed"); } voidFlushbuffer ()throwsIOException {synchronized(lock) {ensureopen (); if(Nextchar = = 0) return; Out.write (CB,0, Nextchar); Nextchar= 0; } } Public voidWriteintCthrowsIOException {synchronized(lock) {ensureopen (); if(Nextchar >=nchars) Flushbuffer (); Cb[nextchar++] = (Char) C; } } Private intMinintAintb) {if(A < b)returnA; returnb; } Public voidWriteCharCbuf[],intOffintLenthrowsIOException {synchronized(lock) {ensureopen (); if((Off < 0) | | (Off > Cbuf.length) | | (Len < 0) | |((Off+ len) > cbuf.length) | | ((off + len) < 0)) { Throw Newindexoutofboundsexception (); } Else if(len = = 0) { return; } if(Len >=nchars) { /*If The request length exceeds the size of the output buffer, flush the buffer and then write the DA Ta directly. In this buffered streams would cascade harmlessly. */Flushbuffer (); Out.write (Cbuf, off, Len); return; } intb = off, t = off +Len; while(b <t) {intd = min (Nchars-nextchar, T-b); System.arraycopy (CBUF, B, CB, Nextchar, D); b+=D; Nextchar+=D; if(Nextchar >=nchars) Flushbuffer (); } } } Public voidWrite (String s,intOffintLenthrowsIOException {synchronized(lock) {ensureopen (); intb = off, t = off +Len; while(b <t) {intd = min (Nchars-nextchar, T-b); S.getchars (b, B+D, CB, Nextchar); b+=D; Nextchar+=D; if(Nextchar >=nchars) Flushbuffer (); } } } Public voidNewLine ()throwsIOException {write (lineseparator); } Public voidFlush ()throwsIOException {synchronized(lock) {flushbuffer (); Out.flush (); }} @SuppressWarnings ("Try") Public voidClose ()throwsIOException {synchronized(lock) {if(out = =NULL) { return; } Try(Writer w =Out ) {Flushbuffer (); } finally{ out=NULL; CB=NULL; } } }}
It seems there is no novelty ... Should be my daoxing too wife shallow, don't see = =
You can see that bufferedwriter is called buffered because it uses an array of type char as the cache (private char cb[];).
Write operations do not immediately write to the current stream of characters, but first write to the buffer.
Java.io.BufferedWriter API and Source code interpretation