0

2 つのファイルをブロックごとに比較しようとしています。ブロックが等しい場合 - 次のブロックを取得して比較します。最終ブロックが等しい場合 - true を返します。他のすべてのバリアント - false を返します。次のブロックを正しく取得する方法と、ファイルの終わりを取得する方法がわかりません。

private static boolean getBlocks(File file1, File file2, int count) throws IOException {
    RandomAccessFile raf1 = new RandomAccessFile(file1, "r");
    RandomAccessFile raf2 = new RandomAccessFile(file2, "r");
    int point = count * 512;
    FileChannel fc1 = raf1.getChannel();
    FileChannel fc2 = raf2.getChannel();
    MappedByteBuffer buffer1 = fc1.map(FileChannel.MapMode.READ_ONLY, point, 512);
    MappedByteBuffer buffer2 = fc2.map(FileChannel.MapMode.READ_ONLY, point, 512);
    byte[] bytes1 = new byte[512];
    byte[] bytes2 = new byte[512];
    buffer1.get(bytes1);
    buffer2.get(bytes2);
    if (bytes1.length == bytes2.length) {
        for (int i = 0; i < bytes1.length; i++) {
            if(bytes1[i] != bytes2[i]) {
                return false;
            }
        }
        if (true) {
            count++;
            getBlocks(file1, file2, point);
        }
    }
    buffer1.clear();
    buffer2.clear();
    return true;
}
4

1 に答える 1

0

EOF に達するまでファイルをバイト単位で読み取る方法は次のとおりです

これを使用する場合MappedByteBufferは、答えになるはずです: https://stackoverflow.com/a/12509314/1321564

于 2014-11-09T11:48:10.147 に答える