system.in

Java: use NIO with System.in [duplicate]

柔情痞子 提交于 2019-12-04 07:40:11
This question already has answers here : How to get SelectableChannel from an InputStream? (2 answers) Closed 3 years ago . Is it possible to use NIO with System.in? I would like to somehow treat 'stdin' as a selectable channel. Has anyone found a way to do this? Jason S duplicate: How to get SelectableChannel from an InputStream? hmmm... on second thought, for stdin there may be a solution. I found this reference: http://www.javafaq.nu/java-example-code-346.html and in particular: SystemInPipe.java (class which encapsulates stdin as a selectable channel) I don't know about a SelectableChannel

Cygwin encoding difficulties

瘦欲@ 提交于 2019-12-02 02:02:01
Not sure whether this is a programming problem. I began to suspect so... but then I ran the Java program (executable jar) in question in a Windows console instead of a Cygwin one... and it ran fine: output accents fine, accented input accepted fine. So what follows applies only to the Cygwin console. I'm processing some French text. When accented characters are printed ( System.out ) a sort of "hashed box" is printed instead. I saw another question here about this but there was no solution or proper explanation given. And when I enter accented characters these are read in incorrectly (Java

Cygwin encoding difficulties

时光怂恿深爱的人放手 提交于 2019-12-01 08:15:26
问题 Not sure whether this is a programming problem. I began to suspect so... but then I ran the Java program (executable jar) in question in a Windows console instead of a Cygwin one... and it ran fine: output accents fine, accented input accepted fine. So what follows applies only to the Cygwin console. I'm processing some French text. When accented characters are printed ( System.out ) a sort of "hashed box" is printed instead. I saw another question here about this but there was no solution or

How can I fix an “IOException: Stream closed” exception using System.in?

你。 提交于 2019-11-27 16:17:51
I'm writing a simple program that reads and processes file content using a BufferedReader . BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); System.out.println("Enter the file name to read"); String fileName = br.readLine(); br.close(); // Process file contents br = new BufferedReader( new InputStreamReader(System.in) ); System.out.println("Enter another file name to read"); fileName = br.readLine(); br.close(); But when I call second br.readLine() to read another file name, I get the following exception: Exception in thread "main" java.io.IOException: Stream closed

Writing data to System.in

我只是一个虾纸丫 提交于 2019-11-27 15:05:20
问题 In our application, we expect user input within a Thread as follows : BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); I want to pass that part in my unit test so that I can resume the thread to execute the rest of the code. How can I write something into System.in from junit? 回答1: What you want to do is use the method setIn() from System. This will let you pass data into System.in from junit. 回答2: Replace it for the duration of your test: String data = "the text you

Closing BufferedReader and System.in

送分小仙女□ 提交于 2019-11-26 19:11:51
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? Looks like you need: http://commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html Wrap that

Close Scanner without closing System.in

被刻印的时光 ゝ 提交于 2019-11-26 19:11:17
I'm trying to re-factor a large and frequently used part of my application into separate methods to make it easier to maintain. Some of these methods asks the user for input and does input validation, so I've used a Scanner and System.in But when I close my Scanner I also close System.in So my question is, can I only prevent System.in being closed by shielding it with CloseShieldInputStream or should I just start passing a Scanner to the methods? You can just ignore close by implementing custom decorator. public class UnClosableDecorator extends InputStream { private final InputStream

How can I fix an “IOException: Stream closed” exception using System.in?

梦想的初衷 提交于 2019-11-26 18:35:40
问题 I'm writing a simple program that reads and processes file content using a BufferedReader . BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); System.out.println("Enter the file name to read"); String fileName = br.readLine(); br.close(); // Process file contents br = new BufferedReader( new InputStreamReader(System.in) ); System.out.println("Enter another file name to read"); fileName = br.readLine(); br.close(); But when I call second br.readLine() to read another

Reading in from System.in - Java [duplicate]

一个人想着一个人 提交于 2019-11-26 11:48:20
This question already has an answer here: How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer I am not sure how you are supposed to read in from system input from a Java file. I want to be able to call java myProg < file Where file is what I want to be read in as a string and given to myProg in the main method. Any suggestions? corsiKa You can use System.in to read from the standard input. It works just like entering it from a keyboard. The OS handles going from file to standard input. class MyProg { public static void main(String[] args) { Scanner

Close Scanner without closing System.in

核能气质少年 提交于 2019-11-26 07:49:00
问题 I\'m trying to re-factor a large and frequently used part of my application into separate methods to make it easier to maintain. Some of these methods asks the user for input and does input validation, so I\'ve used a Scanner and System.in But when I close my Scanner I also close System.in So my question is, can I only prevent System.in being closed by shielding it with CloseShieldInputStream or should I just start passing a Scanner to the methods? 回答1: You can just ignore close by