用Java的I/O方法讀取相對較大的文件可能是最快的方法是什麼?我當前的解決方案使用BufferedInputStream
保存到分配有1024字節的字節數組中。每個緩衝區都保存在ArrayList
以備後用。整個過程通過一個單獨的線程(可調用接口)調用。在Java中讀取相對較大的字節文件的最快方法
雖然不是很快。
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.");
查看Apache Commons庫以獲取更多選項。爲了確定速度,請看O'Reilly的Java Performance Tuning一書。 – therobyouknow 2012-02-01 10:03:07
目前,您正在忽略read()調用返回的值。 *不要那樣做。* – 2012-02-01 10:06:34