Java read line from console longer than 1024 characters

326 Views Asked by At

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.

Can you please point me to some property where can I change (?internal buffer size?) of 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

There are 1 best solutions below

1
On

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());