这是一个读取充满双打的二进制文件的好方法吗?

时间:2017-06-10 12:51:46

标签: java file binary

我有一个二进制文件列表,我需要读取,然后存储到变量。每个文件都是大量双打的集合。这些文件是用C语言保存的,在Linux下用C语言加载双重类型。现在,我想使用Java读取所有这些文件。这是您可以实现的最快方法吗?在我的电脑中,需要24秒才能读取10个文件(1.5 Mb /文件,194,672个双打/文件)并将它们存储到一个数组中。我在考虑使用某种类型的缓冲区,但我不确定是否应该从乞讨中留下一些字节......

    int i;
    int num_f = 10;
    int num_d = 194672;

    File folder = new File(route);
    File[] listOfFiles = folder.listFiles();

    float double_list[][] = new float[num_f][num_d];

    for (int file = 0; file < listOfFiles.length; file++) {
        if (listOfFiles[file].isFile()) {
            try{
                br = new DataInputStream(new FileInputStream(listOfFiles[file].getAbsolutePath()));
                //We read all file 
                i = 0;
                while(br.available() > 0) {
                    //I know that float != double but I don't think I will lose a huge precision 
                    //as the double numbers stored are in a region [-5,5] and with this way I reduce 
                    //the amount of memory needed. (float) is not cpu consuming (<1s).
                    double_list[file][i++] = (float) br.readDouble();
                }

                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    //Close file
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
     }

1 个答案:

答案 0 :(得分:0)

最后,我可以在Andreas和这个网站(http://pulasthisupun.blogspot.com.es/2016/06/reading-and-writing-binary-files-in.html)的帮助下完成它(检查其他类型的格式!)。对于字节序,默认选项是BIG_ENDIAN代码但是我把无意义的东西作为无穷大数字。尽管如此,有了LITTLE_ENDIAN,我得到了正确的数字!尽管我将来还要做一些测试,以确保我不必从一开始就让一些额外的字节......

BTW,花费的时间:0.160048575s,还不错;)

int i;
int num_f = 10;
int num_d = 194672;

File folder = new File(route);
File[] listOfFiles = folder.listFiles();

float double_list[][] = new float[num_f][num_d];

for (int file = 0; file < listOfFiles.length; file++) {
    if (listOfFiles[file].isFile()) {
        try{
           fc = (FileChannel) Files.newByteChannel(Paths.get(listOfFiles[file].getAbsolutePath()), StandardOpenOption.READ);
           byteBuffer = ByteBuffer.allocate((int)fc.size());
           byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
           fc.read(byteBuffer);
           byteBuffer.flip();

           buffer = byteBuffer.asDoubleBuffer();
           ((DoubleBuffer)buffer).get(double_list[file]);
           byteBuffer.clear();

           fc.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                //Close file
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 }