public class BufferedWriter extends Writer
可以指定缓冲区大小,也可以接受默认大小。默认是足够大的用于大多数目的。
提供了一种newline()方法,利用平台自身观念的行分隔符由系统性line.separator定义。并不是所有的平台都使用换行符('\n')终止线。调用此方法终止各输出线因此宁愿写一个换行符直接。
在一般情况下,一个作家发送其输出立即到底层字符或字节流。除非及时输出是必需的,最好是用BufferedWriter围绕其write()操作可能是昂贵的任何作家,如filewriters和outputstreamwriters。例如,
PrintWriter出来=新PrintWriter(新BufferedWriter(新FileWriter(“foo”)));缓存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输出缓冲区的大小,一个正整数
sz <= 0
IllegalArgumentException
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
通常,此方法将给定数组中的字符存储到该流的缓冲区中,并根据需要将缓冲区刷新到基础流中。如果所请求的长度至少和缓冲区一样大,那么这个方法将刷新缓冲区,并直接将字符写入到基础流中。因此BufferedWriters不会复制不必要的数据冗余。
write 方法重写,继承类
Writer
cbuf -字符数组
off偏移开始读取字符的
len数字写
IOException如果I/O错误发生
public void write(String s, int off, int len) throws IOException
如果该len参数值为负则没有书写文字。这是违反本法在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.