用这个给你举例:
FileOutputStream fos = new FileOutputStream("f:\william");
OutputStreamWriter osw = newOutputStreamWriter(fos);
BufferedWriter bw = newBufferedWriter(osw);
bw.write("hello world!");
看看源码:JDK中BufferedWriter流关闭时的代码
public void close() throws IOException {
synchronized (lock) {
if (out == null) {
return;
}
try {
flushBuffer(); //注意
} finally {
out.close();
out = null;
cb = null;
}
}
}
}、在close一个流的时候,它会执行一次flush操作,因此,当把BufferedWriter.close()时,它会把自身的内容,一次性flush出去。如果此时OutputStreamWriter已经关闭了,那么BufferedWriter自身的内容流不出去,因此会抛出java.io.IOException: Stream closed。
希望对你有帮助 |