我想通过标准输入(控制台)向我的程序发送输入,但是当我这样做时,它又发送了两个字符,回车符和换行符。我不希望我的程序正在阅读这些。有没有一种方法可以在不发送这些字符的情况下发送我键入的内容?
这是我的代码:
import java.io.IOException;
public class Main {
public static void main(String[] args) {
while (true) {
try {
System.out.println(System.in.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
控制台输出:
a <- Here I typed 'a' and hit enter
97 <- These three came from stdout
13 <-
10 <-
最佳答案
您可以使用设计为一次读取一行的类之一,例如 BufferedReader
.这将拉入文本行,但解析出行终止字符:
while (true) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
请记住,由于这是拉入整行,因此输出将是 String
,而不是逐字节输出。
关于java - Eclipse stdin 不发送回车和换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25812923/