从Java中的文件中读取大量数据 [英] Read large amount of data from file in Java

查看:324
本文介绍了从Java中的文件中读取大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文字文件包含以下格式中的 1 000 002 数字:

I've got text file that contains 1 000 002 numbers in following formation:

123 456
1 2 3 4 5 6 .... 999999 100000

现在我需要读取该数据并将其分配给 int 变量(前两个数字)和其余所有数据( 1 000 000 数字)到一个数组 int []

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.

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

它的作用是例外,但需要 7500 ms 执行。我需要在几百毫秒内获取该数据。

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.

使用 BufferedReader.readLine() String.split()我在中获得了相同的结果1700毫秒,但它仍然太多。

Using BufferedReader.readLine() and String.split() I got the same results in about 1700 ms, but it's still too many.

如何在不到1秒的时间内读取该数据量?最终结果应该等于:

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 };



根据trashgod回答:



StreamTokenizer 解决方案很快(大约需要1400毫秒),但仍然太慢:

According to trashgod answer:

StreamTokenizer solution is fast (takes about 1400 ms) but it's still too slow:

StreamTokenizer st = new StreamTokenizer(new FileReader("./test_grz"));
st.nextToken();
int n = (int) st.nval;

st.nextToken();
int t = (int) st.nval;

int array[] = new int[n];

for (int i = 0; st.nextToken() != StreamTokenizer.TT_EOF; i++) {
    array[i] = (int) st.nval;
}

PS。无需验证。我100%确定 ./ test_grz 文件中的数据是否正确。

PS. There is no need for validation. I'm 100% sure that data in ./test_grz file is correct.

推荐答案

感谢您的每一个答案,但我已经找到了符合我标准的方法:

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;
}

只需 300 ms 即可阅读1百万整数!

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

这篇关于从Java中的文件中读取大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆