简体   繁体   English

用Java读取二进制文件

[英]Reading a binary file in Java

I have a comparatively long file of unsigned integers (64 bits each, 0.47GB file) that I need to read and store in an array. 我有一个相对较长的无符号整数文件(每个64位,0.47GB文件),我需要读取并存储在一个数组中。 After some brain racking I wound up using the type long, since everything in Java is signed (correct me if I'm wrong, please) and I couldn't think of a better alternative. 经过一段时间的脑力劳动后,我终于使用了这种类型,因为Java中的所有内容都已签名(请纠正我,如果我错了,请),我想不出更好的选择。 Anyhow, the array only has to be sorted, so the precise values of the original numbers are not of the utmost importance. 无论如何,只需要对数组进行排序,因此原始数字的精确值并不是最重要的。 We're supposed to measure the efficiency of the sorting algorithm, nothing more. 我们应该测量排序算法的效率,仅此而已。 However, I came up against a brick wall when I actually came to reading the file (my code below). 然而,当我真正来阅读文件时,我遇到了一堵砖墙(我的代码如下)。

public class ReadFileTest {
    public static void main(String[] args) throws Exception {
        String address = "some/directory";
        File input_file = new File (address);
        FileInputStream file_in = new FileInputStream(input_file);
        DataInputStream data_in = new DataInputStream (file_in );

        long [] array_of_ints = new long [1000000];
        int index = 0;

        long start = System.currentTimeMillis();

        while(true) {
            try {
                long a = data_in.readLong();
                index++;
                System.out.println(a);
            }
            catch(EOFException eof) {
                System.out.println ("End of File");
                break;
            }
        }

        System.out.println(index);
        System.out.println(System.currentTimeMillis() - start);
    }
}

It goes on and on forever, and I usually step out to have lunch while the programme's reading. 它会一直持续下去,我通常会在节目阅读时走出去吃午饭。 All in all 20 minutes is the fastest I've achieved so far. 总共20分钟是迄今为止我所取得的最快成绩。 A course mate bragged today that his programme read it in 4 sec. 今天的一位高级球员吹嘘自己的节目在4秒内完成。 He's working in C++ and I know that C++ is faster than Java, but this is ridiculous. 他在C ++工作,我知道C ++比Java快,但这很荒谬。 Could somebody, please, tell me what I'm doing wrong here. 有人可以告诉我这里我做错了什么。 I can't blame it on the language or the machine, so it must be me. 我不能责怪语言或机器,所以一定是我。 From what I can see, though, the Java tutorials use exactly the same class, ie DataInputStream . 但是,从我所看到的,Java教程使用完全相同的类,即DataInputStream I also saw FileChannels being recommended a couple of times. 我也看过FileChannels被推荐了几次。 Are they the only way out? 他们是唯一的出路吗?

You should use buffered input, something like: 您应该使用缓冲输入,例如:

new DataInputStream(
    new BufferedInputStream(
        new FileInputStream(new File(input_file))))

Want to object of the file: 想要文件的对象:

new ObjectInputStream(
    new BufferedInputStream(
        new FileInputStream(new File(file_name))))

More about difference 更多关于差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM