关闭BufferedReader和System.in [英] Closing BufferedReader and System.in

查看:129
本文介绍了关闭BufferedReader和System.in的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Reader rdr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(rdr);
String s;
s = br.readLine();
br.close();
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
System.out.print(s);

我注意到,如果我关闭 BufferedReader ,我将无法再从键盘插入输入,因为 System.in 以某种方式关闭。无论如何我可以保留 br.close()(我需要这个才能删除文件),然后从键盘添加更多输入?

I've noticed that if I close the BufferedReader, I won't be able to insert input from the keyboard anymore, as System.in is somehow closed. Is there anyway I can keep br.close() (I need that in order to delete a file) and then add more input from the keyboard?

推荐答案

看起来你需要:

http://commons.apache.org/io/apidocs/org/apache/commons/io/ input / CloseShieldInputStream.html

在制作读者之前将其包裹在System.in周围,然后一切都会好的,因为你不会那样做你正在使用FileInputStream。

Wrap that around System.in before making your reader, and then all will be well, since you won't do that when you are using a FileInputStream.

这篇关于关闭BufferedReader和System.in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆