我正在尝试获取用户输入的文本,但似乎对用户可以输入的字符数有限制。我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()
andread(char[] cbuf, int off, int len)
方法,但我仍然无法输入超过 1011 个字符。当我使用Scanner
而不是BufferedReader
.
然后我得出结论,它与我的系统有关,所以我尝试在不同的服务器上运行相同的代码,发现当时我最多可以输入 4049 个字符(不包括末尾的换行符)。
有谁知道这是为什么,以及我可以改变什么以便我可以通过阅读更多字符System.in
?如有必要,我可以发布更多代码。