import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class IOTest {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("d:\\test.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("java IO 流的关闭顺序测试");
//这种关闭顺序会报异常: java.io.IOException: Stream closed
fos.close();
osw.close();
bw.close();
//这种关闭顺序则不会报异常
//bw.close();
//osw.close();
//fos.close();
}
}
所有打开的IO流必须按照第二种方式依次关闭吗? 为啥第一种会报 Stream closed 异常? |