0
我有一個問題,用這種方法IndexOutOfBoundsException異常的字節緩衝區的比較java的
private static boolean getBlocks(File file1, File file2) throws IOException {
FileChannel channel1 = new FileInputStream(file1).getChannel();
FileChannel channel2 = new FileInputStream(file2).getChannel();
int SIZE = (int) Math.min((8192), channel1.size());
int point = 0;
MappedByteBuffer buffer1 = channel1.map(FileChannel.MapMode.READ_ONLY, 0, channel1.size());
MappedByteBuffer buffer2 = channel2.map(FileChannel.MapMode.READ_ONLY, 0, channel2.size());
byte [] bytes1 = new byte[SIZE];
byte [] bytes2 = new byte[SIZE];
while (point < channel1.size() - SIZE) {
buffer1.get(bytes1, point, SIZE);
buffer2.get(bytes2, point, SIZE);
if (!compareBlocks(bytes1, bytes2)) {
return false;
}
point += SIZE;
}
return true;
}
private static boolean compareBlocks (byte[] bytes1, byte[] bytes2) {
for (int i = 0; i < bytes1.length; i++) {
if (bytes1[i] != bytes2[i]) {
return false;
}
}
return true;
}
在結果我在while循環陷入IndexOutOfBoundsException異常。 我怎樣才能解決這個問題,並通過塊來比較兩個文件?
Err,'ByteBuffer'定義了'.equals()'那麼你爲什麼不使用它呢? – fge 2014-12-02 19:49:54
你在哪裏得到什麼IndexOutOfBoundsException?我沒有看到任何可以產生的地方。 – zapl 2014-12-02 20:00:06
哦,如果我嘗試返回buffer1.equals(buffer2),我得到java.io.IOException:映射失敗 – 2014-12-02 20:03:46