BufferedWriter 是缓冲字符输出流。它继承于Writer。它的作用是为其他字符输出流添加一些缓冲功能,能够提高效率。
BufferedWriter是给FileWriter提高效率用的,比如当我们向磁盘中不断的写入字节时或者将一个非常大单位是G的字节数据写入到磁盘的指定文件中,每写入一个字节就要打开一次到这个磁盘的通道,这个结果无疑是恐怖的,而当我们使用BufferedWriter,我们可以在程序中先将要写入到文件中的字符写入到BufferedWriter的内置缓存空间中,然后当达到一定数量时一次性写入。这样做虽然不可能达到一次访问就将所有数据写入磁盘中的效果,但也大大提高了效率和减少了磁盘的访问量!
BufferedReader和BufferedWriter类各拥有8192个字符的缓冲区。当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并放满缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。
//找到目标文件
File file = new File("D:\\jdk_s_d\\out.txt");
//建立缓冲输出流对象,参数true表示追加内容
BufferedWriter bufferedWriter =
new BufferedWriter(
new FileWriter(file,true));
//写出数据
// bufferedWriter.newLine(); //newLine() 换行。
bufferedWriter.write("hello world!!");
// 写入world
bufferedWriter.write("hello world!!", 6, 5);
bufferedWriter.flush();
//关闭资源
bufferedWriter.close();
乱码问题
因为我们使用的是包装FileReader的BufferedReader,包装FileWriter的BufferedWriter。读字符,写字符的时候使用的是默认的字符编码读写的。所以读写文件的时候会出现乱码,可以使用包装InputStreamReader的BufferedReader,包装OutputStreamWriter的BufferedWriter来复制文件,这样就可以支持各种字符编码。
BufferedReader reader = null;
BufferedWriter writer = null;
try
{
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFile), srcEncoding));
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile), destEncoding));
char[] charArray = new char[512];
int size;
while ((size = reader.read(charArray, 0, charArray.length)) != -1)
{
writer.write(charArray, 0, size);
}
} catch (UnsupportedEncodingException | FileNotFoundException e)
{
e.printStackTrace();
}finally
{
if (writer != null)
{
try
{
writer.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
if (reader != null)
{
try
{
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
源码(jdk1.8)
public class BufferedWriter extends Writer {
// 输出流对象
private Writer out;
// 保存“缓冲输出流”数据的字符数组
private char cb[];
// nChars 是cb缓冲区中字符的总的个数
// nextChar是下一个要读取的字符在cb缓冲区中的位置
private int nChars, nextChar;
// 默认字符缓冲区大小
private static int defaultCharBufferSize = 8192;
// 行分割符
private String lineSeparator;
// 构造函数,传入“Writer对象”,默认缓冲区大小是8k
public BufferedWriter(Writer out) {
this(out, defaultCharBufferSize);
}
// 构造函数,传入“Writer对象”,指定缓冲区大小是sz
public BufferedWriter(Writer out, int sz) {
super(out);
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.separator"));
}
// 确保“BufferedWriter”是打开状态
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
// 对缓冲区执行flush()操作,将缓冲区的数据写入到Writer中
void flushBuffer() throws IOException {
synchronized (lock) {
ensureOpen();
if (nextChar == 0)
return;
out.write(cb, 0, nextChar);
nextChar = 0;
}
}
// 将c写入到缓冲区中。
// 先将c转换为char,然后将其写入到缓冲区。
public void write(int c) throws IOException {
synchronized (lock) {
ensureOpen();
// 若缓冲区满了,则清空缓冲,
// 将缓冲数据写入到输出流中。
if (nextChar >= nChars)
flushBuffer();
cb[nextChar++] = (char) c;
}
}
// 返回a,b中较小的数
private int min(int a, int b) {
if (a < b) return a;
return b;
}
// 将字符数组cbuf写入到缓冲中,
// 从cbuf的off位置开始写入,写入长度是len。
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) {
/** 如果请求长度超过输出缓冲区的大小,
* 刷新缓冲区,然后直接写入数据
*/
flushBuffer();
out.write(cbuf, off, len);
return;
}
int b = off, t = off + len;
while (b < t) {
// nChars-nextChar:缓冲区剩余空间
// t-b:[off,len]未写入缓存区的大小
int d = min(nChars - nextChar, t - b);
System.arraycopy(cbuf, b, cb, nextChar, d);
b += d;
nextChar += d;
if (nextChar >= nChars)
flushBuffer();
}
}
}
// 将字符串s写入到缓冲中,
// 从s的off位置开始写入,写入长度是len。
public void write(String s, int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
int b = off, t = off + len;
while (b < t) {
// nChars-nextChar:缓冲区剩余空间
// t-b:[off,len]未写入缓存区的大小
int d = min(nChars - nextChar, t - b);
// String的getChars方法中调用System.arraycopy
// 相当于System.arraycopy(s.toCharArray(), b, cb, nextChar, (b+d)-b)
// 写满缓冲区
s.getChars(b, b + d, cb, nextChar);
b += d;
nextChar += d;
if (nextChar >= nChars)
flushBuffer();
}
}
}
// 将换行符写入到缓冲中
public void newLine() throws IOException {
write(lineSeparator);
}
// 清空缓冲区数据
public void flush() throws IOException {
synchronized (lock) {
flushBuffer();
out.flush();
}
}
// 关闭流
public void close() throws IOException {
synchronized (lock) {
if (out == null) {
return;
}
try {
flushBuffer();
} finally {
out.close();
out = null;
cb = null;
}
}
}
}