running in deadlock while reading from System.in
Submitted by Trickkiste on Fri, 04/30/2004 - 18:52.
hi,
I used the following code to read / write from / to screen :
byte buffer[] = new byte[80];
String input = "";
int read;
do {
try {
read = System.in.read(buffer, 0, 80);
input = new String(buffer, 0, read);
System.out.print(input);
}
catch(IOException e) {e.printStackTrace();}
} while(! input.equals("."+System.getProperty("line.separator")));
but JNode was running into a deadlock,
I also tried :
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
input = stin.Readline();
has anyone a idea (or a better way to read user input ?)?
Andreas
- Login to post comments
Reading a line from System.in
To read a line, I think best practice is:
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
do {
String line = in.readLine();
System.out.println(line);
} while(line.length() > 0);
BUT, GNU classpath has/had a bug (maybe its fixed now) in BufferedReader. So, until its fixed you have to replace BufferedReader with
DataInputStream in=new DataInputStream(System.in);
it does not work...
thanks for your suggestion, but it does not wrok.
I also tried to do this little thing in a thread, but after I started the thread the console got the System.in and my thread did nothing...
Andreas