Read large amount of data from file in Java

后端 未结 7 1330
一生所求
一生所求 2020-12-03 01:36

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

7条回答
  •  隐瞒了意图╮
    2020-12-03 02:19

    Thanks for every answer but I've already found a method that meets my criteria:

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("./path"));
    int n = readInt(bis);
    int t = readInt(bis);
    int array[] = new int[n];
    for (int i = 0; i < n; i++) {
        array[i] = readInt(bis);
    }
    
    private static int readInt(InputStream in) throws IOException {
        int ret = 0;
        boolean dig = false;
    
        for (int c = 0; (c = in.read()) != -1; ) {
            if (c >= '0' && c <= '9') {
                dig = true;
                ret = ret * 10 + c - '0';
            } else if (dig) break;
        }
    
        return ret;
    }
    

    It requires only about 300 ms to read 1 mln of integers!

提交回复
热议问题