BufferedWriter Introduction
BufferedWriter is the buffered character output stream. It inherits from writer.
The role of BufferedWriter is to add some buffering functionality to other character output streams.
BufferedWriter Function List
Constructor
BufferedWriter (Writer out)
BufferedWriter (Writer out, int-sz)
void Close () //closes this stream, But you have to refresh it first.
void Flush () //refreshes the buffer of the stream.
void newline () //writes a line separator.
void Write (char[] cbuf, int off, int len)//Writes a part of the character array.
void Write (int c) //write a single character.
void Write (string s, int off, int len) //write a part of the string.
BufferedWriter Source Analysis (based on jdk1.7.40)
Package java.io;
public class BufferedWriter extends Writer {//Output stream object private Writer out;
The character array that holds the buffered output stream data private char cb[];
Nchars is the total number of characters in the CB buffer//Nextchar is the next character to read in the CB buffer position private int nchars, Nextchar;
Default character buffer size private static int defaultcharbuffersize = 8192;
Row delimiter private String lineseparator;
constructor, passing in the "Writer object", the default buffer size is 8k public bufferedwriter (Writer out) {This (out, defaultcharbuffersize);
}//constructor, passing in "Writer object", specifies that the buffer size is SZ public bufferedwriter (Writer out, int sz) {super);
if (SZ <= 0) throw new IllegalArgumentException ("Buffer size <= 0");
This.out = out;
CB = new Char[sz];
Nchars = sz;
Nextchar = 0; LineSeparator = java.security.AccessController.doPrivileged (New Sun.security.action.GetPropertyAction ("Line.s
Eparator ")); }//ensure "BUfferedwriter "is open state private void Ensureopen () throws IOException {if (out = = null) throw new Ioex
Ception ("Stream closed");
A flush () operation is performed on the buffer, and the data for the buffer is written to the writer Void Flushbuffer () throws IOException {synchronized (lock) {
Ensureopen ();
if (Nextchar = = 0) return;
Out.write (CB, 0, Nextchar);
Nextchar = 0; Write c to the buffer.
Convert C to Char, and then write it to the buffer.
public void write (int c) throws IOException {synchronized (lock) {ensureopen ();
If the buffer is full, the buffer is emptied and the buffered data is written to the output stream.
if (Nextchar >= nchars) Flushbuffer ();
cb[nextchar++] = (char) c;
}//Returns the smaller number in a,b private int min (int a, int b) {if (a < b) return A;
return b;
Writes the character array cbuf to the buffer, writes from the cbuf off position, writes Len. See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/java/public void Write (char cbuf[], int off, int len) throws IOException {synchronized (lock) {
Ensureopen (); if (Off < 0) | | (Off > Cbuf.length) | |
(Len < 0) | | ((off + len) > cbuf.length) | | ((off + len) < 0))
{throw new indexoutofboundsexception ();
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 data directly. In this way buffered streams is cascade harmlessly.
* * Flushbuffer ();
Out.write (Cbuf, off, Len);
Return
int B = off, t = off + len;
while (b < T) {int d = min (Nchars-nextchar, t-b);
System.arraycopy (CBUF, B, CB, Nextchar, D); B + D;
Nextchar = D;
if (Nextchar >= nchars) Flushbuffer ();
Writes the string s to the buffer, writes from the off position of S, writes Len.
public void Write (String s, int off, int len) throws IOException {synchronized (lock) {ensureopen ();
int B = off, t = off + len;
while (b < T) {int d = min (Nchars-nextchar, t-b);
S.getchars (b, B + D, CB, Nextchar);
B + D;
Nextchar = D;
if (Nextchar >= nchars) Flushbuffer ();
Write a newline character to the buffer public void newline () throws IOException {write (lineseparator); }//empty buffer data public void flush () throws IOException {synchronized (lock) {Flushbuffe
R ();
Out.flush ();
} public void Close () throws IOException { Synchronized (lock) {if (out = = null) {return;
try {flushbuffer ();
finally {out.close ();
out = null;
CB = NULL; }
}
}
}
Description: BufferedWriter's source code is very simple, here on the idea of BufferedWriter simple description: BufferedWriter through the character array to buffer the data, when the buffer full or the user calls the Flush () function, It writes the data of the buffer to the output stream.