java - 通过字节 block 比较两个文件java

标签 java compare byte arrays block

我尝试按字节 block 比较两个文件。一 block 一 block 地。但我的循环树有问题。

    public void compare() {
        File file1 = arrayOfFiles.get(i);
        File file2 = arrayOfFiles.get(y);
        if (file1.length() != file2.length()) {
            break;
        }
        else
        {
            for (int z = 0; ; z++) {
                byte[] b1 = getParts(file1, z);
                byte[] b2 = getParts(file2, z);
                if (b1.length != b2.length) {
                    break;
                }
                else
                {
                    for (int x = 0; ; x++) {
                        if (b1[x] != b2[x]) {
                            break;
                        }
                        else
                        {
                            //how can I find the end of file? and compare last [x] of b1 and b2?
                        }
                    }
                }
            }
        }
    }

    private static byte[] getParts(File file, int z) throws IOException {
        byte [] bytes = new byte[1024];
        int point = z * 1024;
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();
        MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, point, 1024);
        buffer.get(bytes);
        buffer.clear();
        return bytes;
    }

是否有另一种方法可以按字节比较两个文件并使用不同大小的 block 进行比较?

最佳答案

为了比较文件的最后一个字节 block ,您的程序可以从较小的修改中受益。从最后一个 block 开始迭代 block 。将 for 子句更改如下以向后迭代。

import java.lang.Math;
import java.nio.channels.FileChannel;
import java.io.File;
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.MappedByteBuffer;

public class Compare
{
    public static final int BLOCK_SIZE = 1024;

    public boolean compare(File file1, File file2)
    {
        //File file1 = arrayOfFiles.get(i);
        //File file2 = arrayOfFiles.get(y);
        boolean equal = file1.length() != file2.length();
        for (int z = getSizeInBlocks(file1) - 1; equal && 0 <= z ; z--)
        {
            MappedByteBuffer b1 = getParts(file1, z);
            MappedByteBuffer b2 = getParts(file2, z);
            if (b1.remaining() != b2.remaining())
            {
                equal = false;
                break;
            }
            else
            {
                for (int x = getBlockSize() - 1; equal && 0 <= x; x--) 
                {
                    if (b1[x] != b2[x])
                    {
                        equal = false;
                        break;
                    }
                    else
                    {
                        //how can I find the end of file? and compare last [x] of b1 and b2?
                    }
                }
            }
        }
        return equal;
    }

    private static int getSizeInBlocks(File file)
    {
        return (int) Math.ceil((double)getBlockSize()/file.length());
    }

    private static int getBlockSize()
    {
        return BLOCK_SIZE;
    }

    private static ByteBuffer getParts(File file, int z)
        throws IOException 
    {
        int point = z * getBlockSize();
        RandomAccessFile raf    = new RandomAccessFile(file, "r");
        FileChannel      fc     = raf.getChannel();
        MappedByteBuffer buffer = fc.map(
                                    FileChannel.MapMode.READ_ONLY, 
                                    point, 
                                    getBlockSize());
        return buffer;
    }
}

关于java - 通过字节 block 比较两个文件java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805900/

相关文章:

c - 在c中每次读取文件多个字节

java - 在不关闭 IDE 的情况下保存/刷新 Eclipse 状态

java - 递归 - Kata 挑战

java - 以 1024 字节的 block 分割 Java 字符串

c++ - std::string 比较(检查字符串是否以另一个字符串开头)

svn - 有没有可以逐行合并的比较工具?

c++ - 获取我的 IP 地址并使用 C++ 将其转换为字节

java - 在 Intellij CE 中,如何显示 Maven 选项卡?

java - 如何从Class的HashSet或HashMap修改变量类

compare - Beyond Compare 软件算法如何工作?