在同一个类中如果一个关于System.in的流关闭了,就不能再开启了,如下程序段:
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
System.out.println(bufr.readLine());
bufr.close();
BufferedReader bufd = new BufferedReader(new InputStreamReader(System.in));
System.out.println(bufd.readLine());
它会报异常:
aaaa
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.read(BufferedInputStream.java:308)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)aaaa
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.itheima.Text.main(Text.java:15)
但是其他的输入流就没有没有这样的问题:
BufferedInputStream f = new BufferedInputStream(new FileInputStream("f:\\2\\a.java"));
byte[] b = new byte[1024];
int len=0;
while((len = f.read(b))!=-1){
System.out.println(new String(b,0,len));
}
f.close();
BufferedInputStream fr = new BufferedInputStream(new FileInputStream("f:\\2\\a.java"));
这个就是正常的,不会包异常,请问这是怎么回事? |