Read large amount of data from file in Java
        Posted  
        
            by Crozin
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Crozin
        
        
        
        Published on 2010-04-22T17:57:45Z
        Indexed on 
            2010/04/22
            18:33 UTC
        
        
        Read the original article
        Hit count: 215
        
Hello
I've got text file that contains 1 000 002 numbers in following formation:
123 456
1 2 3 4 5 6 .... 999999 100000
Now I need to read that data and allocate it to int variables (the very first two numbers) and all the rest (1 000 000 numbers) to an array int[].
It's not a hard task, but - it's horrible slow.
My first attempt was java.util.Scanner:
 Scanner stdin = new Scanner(new File("./path"));
 int n = stdin.nextInt();
 int t = stdin.nextInt();
 int array[] = new array[n];
 for (int i = 0; i < n; i++) {
     array[i] = stdin.nextInt();
 }
It works as excepted but it takes about 7500 ms to execute. I need to fetch that data in up to several hundred of milliseconds.
Then I tried java.io.BufferedReader:
Using BufferedReader.readLine() and String.split() I got the same results in about 1700 ms, but it's still too many.
How can I read that amount of data in less that 1 second? The final result should be equal to:
int n = 123;
int t = 456;
int array[] = { 1, 2, 3, 4, ..., 999999, 100000 };
© Stack Overflow or respective owner