在Java中读取相对较大的字节文件的最快方法 [英] Fastest way of reading relatively huge byte-files in Java
问题描述
BufferedInputStream
保存到分配有1024字节的字节数组中。每个缓冲区都保存在 ArrayList
中以供以后使用。整个过程是通过一个单独的线程(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 =新字节[LIMIT];
}
else else;
System.out.println(FileReader-Elements:+ outputArr.size()+w。+ buffer.length +byte each。);
我会使用一个足够快的内存映射文件(b)在$ {
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $>
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
//完成后
channel.close();
假定文件小于2 GB并且需要10毫秒或更少。 p>
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).
Not very fast though.
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();
This assumes the file is smaller than 2 GB and will take 10 milli-seconds or less.
这篇关于在Java中读取相对较大的字节文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!