我试图让用户输入文字,但似乎用户可以输入的字符数有限制。我使用BufferedReader
来读取System.in
这就是我原来的代码:
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter text.");
while (true){
System.out.print("Text> ");
String line = r.readLine();
if (line == null || line.equalsIgnoreCase("q")) {
break;
}
else {
System.out.println("Response: " + line);
System.out.println("Response length: " + line.length());
}
}
这是我用一些示例文本运行它时得到的结果:
Text> WASHINGTON — Republicans on Thursday vowed a swift and forceful response to the executive action on immigration that President Obama is to announce in a prime-time address, accusing the president of exceeding the power of his office and promising a legislative fight when they take full control of Congress next year. Senator Mitch McConnell of Kentucky, who will become majority leader in January, said in a speech on the Senate floor Thursday morning that Mr. Obama would regret choosing to ignore the wishes of the American people. “If President Obama acts in defiance of the people and imposes his will on the country, Congress will act,” Mr. McConnell said just hours before the president was scheduled to speak to the nation on television. “We’re considering a variety of options. But make no mistake. Make no mistake. When the newly elected representatives of the people take their seats, they will act.” Mr. McConnell did not say what options Republicans were considering, but the party is sharply di
Response: WASHINGTON — Republicans on Thursday vowed a swift and forceful response to the executive action on immigration that President Obama is to announce in a prime-time address, accusing the president of exceeding the power of his office and promising a legislative fight when they take full control of Congress next year. Senator Mitch McConnell of Kentucky, who will become majority leader in January, said in a speech on the Senate floor Thursday morning that Mr. Obama would regret choosing to ignore the wishes of the American people. “If President Obama acts in defiance of the people and imposes his will on the country, Congress will act,” Mr. McConnell said just hours before the president was scheduled to speak to the nation on television. “We’re considering a variety of options. But make no mistake. Make no mistake. When the newly elected representatives of the people take their seats, they will act.” Mr. McConnell did not say what options Republicans were considering, but the party is sharply di
Response length: 1011
Text>
无论文本有多长,它都会在第1011个字符处被切断。 (它在第1012个字符处被切断,但我必须删除该字符才能按ENTER键插入换行符。)
我认为这个问题出现在BufferedReader的readLine()
方法中,所以我尝试使用BufferedReader中的read()
和read(char[] cbuf, int off, int len)
方法,但我仍然无法解决问题。输入超过1011个字符。当我使用Scanner
代替BufferedReader
时,仍会出现同样的问题。
然后我得出的结论是它与我的系统有关,所以我尝试在不同的服务器上运行相同的代码,发现我最多可以输入4049个字符(不包括新行字符)端)。
有谁知道为什么会这样,我可以改变什么,以便我可以通过System.in
阅读更多字符?如有必要,我可以发布更多代码。