java.lang.Object
|
+----java.io.Writer
|
+----java.io.BufferedWriter
将文本写入字符输出流并缓冲字符以便能提供单字符、数组和行的高效写入。
可指定缓冲区尺寸或接受缺省尺寸。该缺省尺寸对大多数用途来说是足够的。
提供了 newLine() 方法, 它使用系统自身的行分隔符概念作为系统定义的 line.separator 特性。并非所有平台使用换行符 ('\n') 来终止行。因此最好调用该方法来终止每个输出行而非直接写换行符。
一般, Writer 直接将其输出发送给基本字符或字节流。除非请求提示输出,建议将 BufferedWriter 和任何代价高昂 write() 操作的 Writer 打包,如 FileWriters 和 OutputStreamWriters 。例如,
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
将 PrintWriter 的输出缓冲到文件中。若无缓冲,则每个 print() 方法的调用将字符转换成字节,然后直接写入文件中,这是非常低效的。
public BufferedWriter(Writer out)
public BufferedWriter(Writer out,
int sz)
public void write(int c) throws IOException
public void write(char cbuf[],
int off,
int len) throws IOException
通常该方法将给定数组中的字符存储到该流的缓冲区中,需要时可将缓冲区刷新为基本输出流。若请求长度至少与该流的缓冲区同样大,则该方法将刷新缓冲区并直接将字符写入基本输出流。因此冗余的 BufferedWriter
不必复制不必要的数据。
public void write(String s,
int off,
int len) throws IOException
public void newLine() throws IOException
public void flush() throws IOException
public void close() throws IOException