1
我有一個類比較器,比較我的文件與不同的算法。 在這部分我嘗試從文件中獲取一個字節塊,以便與另一個文件的塊進行比較。緩衝區下溢異常java
public class CompareFiles {
private byte[] getBytesFromFile(File file) throws IOException {
long BUFFER_SIZE = 4 * 1024;
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, BUFFER_SIZE);
byte[] bytes = new byte[(int) fc.size()];
buffer.get(bytes);
buffer.clear();
return bytes;
}
}
如果我嘗試使用一個在大的文件,我得到
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.DirectByteBuffer.get(Unknown Source)
at java.nio.ByteBuffer.get(Unknown Source)
我猜,我犯錯,我的心靈,讓以錯誤的方式比較塊。