Search code examples
javafilebufferedreader

Fastest way to read a large number from text file


So I have a very large number (15 million digits) stored in a text file and I'm using this method to read the number

 BufferedReader Br1 = null;
    StringBuilder Final = new StringBuilder("");

    System.out.println("Loading......");

    Br1 = new BufferedReader (new FileReader("NumberFind.txt"));
    String Line = Br1.readLine();
    while(Line != null) {
        Final.append(Line);
        Line = Br1.readLine();
    }
    sum1 = new BigInteger(Final.toString());
    Br1.close();
            System.out.println("Loaded");

This works but it takes about 45 minutes to load the whole number, is there a faster way to load this?


Solution

  • Your file is only 14.3 megabytes if it's just got that one number in it. I don't know what quirk of BufferedReader, BigInteger, etc. causes a 45 minute load, but it's probably just that line-reading loop. You should be able to read the whole file into memory in seconds, not minutes.

    Try reading the entire file (or possibly just the section containing the number) into a string without BufferedReader. See FileReader.readAsBinaryString() for accomplishing that.

    After you have the number in memory as a string, you should be able to construct a new BigInteger by passing the string param to it like you are already doing in your above code sample.

    If this doesn't solve everything, and you want more insights, I would recommend narrowing down where that 45 minutes of delay is happening. I'm guessing it's in your line-reading loop, but I could be wrong. If you are in some environment, e.g. embedded device, with unusual constraints on CPU, disk read time, etc. that may be a factor.