来自控制台的Java读取行超过1024个字符

时间:2021-11-21 20:47:30

I have the following code, first branch is for file reading (works perfectly for file lines of any size), but is I try the same for console input, the reading blocks after 1024 characters. As there is no difference besides the source stream, I suppose that there is some limitation in System.in.

我有以下代码,第一个分支是文件读取(适用于任何大小的文件行),但是我尝试相同的控制台输入,1024个字符后的读取块。由于源流之外没有区别,我认为System.in存在一些限制。

Can you please point me to some property where can I change (?internal buffer size?) of System.in.

你能指点一下我可以改变System.in的某个属性(?内部缓冲区大小?)。

Thanks!

        if (input != null) {
            reader = new MyReader(new BufferedReader(new InputStreamReader(new FileInputStream(input))));
        } else {
            reader = new MyReader(new BufferedReader(new InputStreamReader(System.in)));
        }

1 个解决方案

#1


-1  

Maybe you can do it like this:

也许你可以这样做:

StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch = System.in.read()) != '\n') {
    sb.append((char)ch);
}
System.out.println(sb.toString());

#1


-1  

Maybe you can do it like this:

也许你可以这样做:

StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch = System.in.read()) != '\n') {
    sb.append((char)ch);
}
System.out.println(sb.toString());