【发布时间】:2017-08-22 10:44:43
【问题描述】:
在处理 BufferedOutputStream 时发现,当我们在关闭流后对其进行写入时,它不会抛出 IOException。
为了验证我的结果,我检查了FileOutputStream,发现在关闭它后尝试在上面写字时它正在抛出IOException。
public class Test {
public static void main(String[] args) {
try {
// Created a byte[] barry1 barry2
byte[] barry1 = { '1', '3' };
byte[] barray2 = { '2', '4' };
OutputStream os = new BufferedOutputStream(
new FileOutputStream("abc.txt", false));
// Writing to stream
os.write(barry1);
os.close();
os.write(barray2); // this suceeds - bug
os = new FileOutputStream("abc.txt", true);
//Writing to stream
os.write(barry1);
os.close();
os.write(barray2); // crashes here, correct.
} catch (Exception e) {
e.printStackTrace();
}
}
}
有人可以帮助我吗,为什么这种行为不同?
【问题讨论】:
标签: java file-io fileoutputstream bufferedinputstream