java.io.IOException:Streamclosed解决办法
1.出现这个bug的⼤体逻辑代码如下:
1private static void findMovieId() throws Exception {
2 File resultFile = new File("C:\\");
3
4 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(resultFile));
5 BufferedWriter bufferWriter = new BufferedWriter(outputStreamWriter);
6
7 outputStreamWriter.close();
8 bufferWriter.close();
9 }
2.报错⼤体信息如下:
java stream3.解决⽅式:
原因:
bufferwriter流使⽤了outputStreamWriter流,所以关闭bufferwriter流也会关闭outputStreamWriter流;但是下⾯有使⽤outputStreamWriter 流,相当于使⽤⼀个关闭了的流。解决办法是换下位置就可以了.
代码改为:
private static void findMovieId() throws Exception {
File resultFile = new File("C:\\");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(resultFile));
BufferedWriter bufferWriter = new BufferedWriter(outputStreamWriter);
bufferWriter.close();
outputStreamWriter.close();
}
同理:FileWriter和BufferWriter之间的关系:
FileWriter fileWriter=new FileWriter(file);
BufferWriter bufferWriter=new BufferedWriter(fileWriter);
这⾥关闭流要从⼤到⼩关闭,BufferWriter包装了FileWriter,BufferWriter"⼤",FileWriter"⼩"
FileWriter.close();
BufferWriter.close();
这样的顺序就是不⾏,FileWriter流⾃⼰关闭了。然后BufferWriter流⼜把FileWriter流关闭⼀次就会报错java.io.IOException: Stream closed.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论