在 Java 中读取相对较大的字节文件的最快方法 [英] Fastest way of reading relatively huge byte-files in Java

查看:16
本文介绍了在 Java 中读取相对较大的字节文件的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Java 的 I/O 方法读取相对较大的文件的最快方法是什么?我当前的解决方案使用 BufferedInputStream 保存到分配给它的 1024 个字节的字节数组.然后将每个缓冲区保存在 ArrayList 中供以后使用.整个过程通过一个单独的线程(callable-interface)调用.

what's the probably fastest way of reading relatively huge files with Java's I/O-methods? My current solution uses the BufferedInputStream saving to an byte-array with 1024 bytes allocated to it. Each buffer is than saved in an ArrayList for later use. The whole process is called via a separate thread (callable-interface).

虽然不是很快.

    ArrayList<byte[]> outputArr = new ArrayList<byte[]>();      
    try {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream (dir+filename));

        byte[] buffer = new byte[LIMIT]; // == 1024 
            int i = 0;
            while (reader.available() != 0) {
                reader.read(buffer);
                i++;
                if (i <= LIMIT){
                    outputArr.add(buffer);
                    i = 0;
                    buffer = null;
                    buffer = new byte[LIMIT];
                }
                else continue;              
            }

         System.out.println("FileReader-Elements: "+outputArr.size()+" w. "+buffer.length+" byte each.");   

推荐答案

我会使用一个内存映射文件,它的速度足以在同一个线程中完成.

I would use a memory mapped file which is fast enough to do in the same thread.

final FileChannel channel = new FileInputStream(fileName).getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

// when finished
channel.close();

这假设文件小于 2 GB,并且需要 10 毫秒或更短的时间.

This assumes the file is smaller than 2 GB and will take 10 milli-seconds or less.

这篇关于在 Java 中读取相对较大的字节文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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