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

查看:20
本文介绍了关闭 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天全站免登陆