Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing BufferedReader and 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);

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?

like image 788
Bogdan Tomi Avatar asked Nov 20 '11 19:11

Bogdan Tomi


1 Answers

Looks like you need:

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

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.

like image 83
bmargulies Avatar answered Oct 02 '22 01:10

bmargulies