How to input a line longer than 4095 characters in Java?

I have no idea why the length of l String is no more than 4095 no matter how long the line I give as input is. I really have no idea where is the mistake. I think it is a terminal rule or something, but how can I bypass that in Java? As you can see I use a BufferedReader and it’s readLine() method and I need to read lines of huge proportions. Ran out of options already.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Krazki {
    public static int solve() {
        try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String l = br.readLine();
        int n = Integer.parseInt(l.split("\s")[0]);
        int m =  Integer.parseInt(l.split("\s")[1]);
        if(n < m) {
            return 0;
        }
        int[] width = new int[n + 2];
        width[0] = Integer.MAX_VALUE;
        l = br.readLine();
        String[] line = l.split("\s", n);
        for(int i = 1; i <= n;i++){
            width[i] = Math.min(width[i-1], Integer.parseInt(line[i-1]));
        }
        width[n + 1] = 0;
        int res = n + 1;
        line = br.readLine().split("\s");
        for(int i = 0; i < m; i++){
            if(res > 0) {
                res--;
            }
            while(width[res] < Integer.parseInt(line[i])) {
                res--;
            }
        }
        br.close();
        return res;
        }
        catch(Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}

Answer

There is (virtually1) no limit on the length of a line that you can input in Java.

If you are seeing a line length limit of 4096 characters, it is probably imposed by the command shell or console application that you are using to enter the data. That is not a problem that can be solved directly in a Java application.

The line length limit is typically OS specific, and needs to be addresses by OS specific means.

However, a more practical approach would to change your application to:

  • take input from a file, where there should not be any line length limits, or
  • implement an line continuation syntax that allows the user to input overly long lines as multiple lines.

1 – An application probably cannot cope with a line longer than ~231 characters, because String and StringBuffer use arrays and arrays are limited to that many elements. In addition, an application would typically need enough memory to hold the complete line.