vladr..
5
不幸的是,这是不可能以便携的方式:
在Windows上,从System.in读取将被阻止,直到enter被按下,即使您不使用BufferedReader.箭头将循环显示命令历史记录.亲自尝试一下:
import java.io.*;
public class KeyTest {
public static void main(String[] argv) {
try {
InputStreamReader unbuffered = new InputStreamReader(System.in);
for (int i = 0; i < 10; ++i) {
int x = unbuffered.read();
System.out.println(String.format("%08x", x));
}
} catch (Exception e) {
System.err.println(e);
}
}
}
使用Console该类的相同问题(Windows下缓冲的输入,Windows解释的箭头键):
import java.io.*;
public class KeyTest2 {
public static void main(String[] argv) {
try {
Console cons = System.console();
if (cons != null) {
Reader unbuffered = cons.reader();
for (int i = 0; i < 10; ++i ) {
int x = unbuffered.read();
System.out.println(String.format("%08x", x));
}
}
} catch (Exception e) {
System.err.println(e);
}
}
}