[英]Fastest way to incrementally read a large file
當給出MAX_BUFFER_SIZE的緩沖區和遠遠超過它的文件時,如何:
RandomAccessFile aFile = new RandomAccessFile(fileName, "r");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(CAPARICY);
int bytesRead = inChannel.read(buffer);
buffer.flip();
while (buffer.hasRemaining()) {
buffer.get();
}
buffer.clear();
bytesRead = inChannel.read(buffer);
aFile.close();
InputStream in = new FileInputStream(fileName);
long length = fileName.length();
if (length > Integer.MAX_VALUE) {
throw new IOException("File is too large!");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + fileName);
}
in.close();
事實證明, 與NIO做同樣的事情 , 常規IO的速度要快100倍 。 我錯過了什么嗎? 這是預期的嗎? 有沒有更快的方法來讀取緩沖區塊中的文件?
最終,我正在處理一個大文件,我沒有記憶可以一次閱讀。 相反,我想以塊的形式逐步讀取它,然后用於處理。
如果你想讓你的第一個例子更快
FileChannel inChannel = new FileInputStream(fileName).getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY);
while(inChannel.read(buffer) > 0)
buffer.clear(); // do something with the data and clear/compact it.
inChannel.close();
如果你想要它更快。
FileChannel inChannel = new RandomAccessFile(fileName, "r").getChannel();
MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
// access the buffer as you wish.
inChannel.close();
對於最大2 GB的文件,這可能需要10 - 20微秒。
假設您需要立即將整個文件讀入內存(正如您當前所做的那樣),讀取較小的塊或NIO都不會幫助您。
實際上,您可能最好閱讀更大的塊 - 您的常規IO代碼會自動為您執行此操作。
您的NIO代碼目前較慢,因為您一次只讀取一個字節(使用buffer.get();
)。
如果你想以塊的形式進行處理 - 例如,在流之間進行傳輸 - 這是在沒有NIO的情況下執行它的標准方法:
InputStream is = ...;
OutputStream os = ...;
byte buffer[] = new byte[1024];
int read;
while((read = is.read(buffer)) != -1){
os.write(buffer, 0, read);
}
這使用的緩沖區大小僅為1 KB,但可以傳輸無限量的數據。
(如果你擴展你的答案,詳細說明你在功能層面上的實際目的,我可以進一步改進這個以獲得更好的答案。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.