public class BufferedWriter extends Writer
可以指定缓冲区大小,或者可以接受默认大小。 默认值足够大,可用于大多数用途。
提供了一个newLine()方法,它使用平台自己的系统属性line.separator定义的行分隔符概念。 并非所有平台都使用换行符('\ n')来终止行。 因此,调用此方法来终止每个输出行,因此优选直接写入换行符。
一般来说,Writer将其输出立即发送到底层字符或字节流。 除非需要提示输出,否则建议将BufferedWriter包装在其write()操作可能很昂贵的Writer上,例如FileWriters和OutputStreamWriters。 例如,
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
将缓冲PrintWriter的输出到文件。
没有缓冲,每次调用print()方法都会使字符转换为字节,然后立即写入文件,这可能非常低效。
| Constructor and Description |
|---|
BufferedWriter(Writer out)
创建使用默认大小的输出缓冲区的缓冲字符输出流。
|
BufferedWriter(Writer out, int sz)
创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。
|
public BufferedWriter(Writer out)
out - 作家
public BufferedWriter(Writer out, int sz)
out - 作家
sz - 输出缓冲区大小,正整数
IllegalArgumentException - 如果
sz <= 0
public void write(int c)
throws IOException
write在
Writer
c - int指定要写入的字符
IOException - 如果发生I / O错误
public void write(char[] cbuf,
int off,
int len)
throws IOException
通常,此方法将给定数组中的字符存储到此流的缓冲区中,根据需要将缓冲区刷新到底层流。 然而,如果请求的长度至少与缓冲区一样大,那么这个方法将刷新缓冲区,并将字符直接写入底层流。 因此冗余BufferedWriter将不会不必要地复制数据。
write在
Writer
cbuf - 一个字符数组
off - 开始读取字符的偏移量
len - 要写入的
len数
IOException - 如果发生I / O错误
public void write(String s, int off, int len) throws IOException
如果len参数的值为负,则不会写入任何字符。 这与superclass中的这种方法的规范相反 , superclass要求抛出一个IndexOutOfBoundsException 。
write在
Writer
s - 要写入的字符串
off - 开始读取字符的偏移量
len - 要写入的字符数
IOException - 如果发生I / O错误
public void newLine()
throws IOException
IOException - 如果发生I / O错误
public void flush()
throws IOException
flush在界面
Flushable
flush在
Writer
IOException - 如果发生I / O错误
public void close()
throws IOException
Writer复制
close在界面
Closeable
close在界面
AutoCloseable
close在
Writer
IOException - 如果发生I / O错误
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.