You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java对比两个不可读的.gfx文件并找出差异?

How to Pinpoint Exact Byte-Level Differences Between Two .gfx Files in Java

Absolutely! Since you’ve already confirmed the files differ (via Files.readAllBytes() and FileUtils.contentEquals()) but can’t locate where, a byte-by-byte comparison is the perfect solution. Here’s a practical, step-by-step approach with Java code to find the exact differing positions and values:

Core Approach

Your files have the same size, so we can iterate through each byte index in both files, compare the bytes at each position, and log any discrepancies. For binary files like .gfx, displaying bytes in hexadecimal makes it easier to interpret differences (e.g., distinguishing metadata changes from actual graphic data).

Java Implementation

Here’s a reusable utility method to detect and report differences clearly:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class FileByteDifferencer {

    public static void main(String[] args) {
        String file1Path = "path/to/your/first.gfx";
        String file2Path = "path/to/your/second.gfx";
        
        try {
            List<ByteDifference> differences = findByteDifferences(file1Path, file2Path);
            
            if (differences.isEmpty()) {
                System.out.println("No differences found between the files.");
            } else {
                System.out.printf("Found %d differences:%n", differences.size());
                for (ByteDifference diff : differences) {
                    System.out.printf("Position %d (0-indexed): File1 = 0x%02X, File2 = 0x%02X%n",
                            diff.index, diff.file1Byte, diff.file2Byte);
                }
            }
        } catch (Exception e) {
            System.err.println("Error comparing files: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static List<ByteDifference> findByteDifferences(String file1Path, String file2Path) throws Exception {
        byte[] file1Bytes = Files.readAllBytes(Paths.get(file1Path));
        byte[] file2Bytes = Files.readAllBytes(Paths.get(file2Path));
        
        // Sanity check: confirm files have matching sizes (you noted this, but better safe)
        if (file1Bytes.length != file2Bytes.length) {
            throw new IllegalArgumentException(
                String.format("Files have different sizes: %d vs %d bytes", file1Bytes.length, file2Bytes.length)
            );
        }
        
        List<ByteDifference> differences = new ArrayList<>();
        
        for (int i = 0; i < file1Bytes.length; i++) {
            byte b1 = file1Bytes[i];
            byte b2 = file2Bytes[i];
            
            if (b1 != b2) {
                differences.add(new ByteDifference(i, b1, b2));
            }
        }
        
        return differences;
    }

    // Helper class to store difference details neatly
    private static class ByteDifference {
        int index;
        byte file1Byte;
        byte file2Byte;
        
        public ByteDifference(int index, byte file1Byte, byte file2Byte) {
            this.index = index;
            this.file1Byte = file1Byte;
            this.file2Byte = file2Byte;
        }
    }
}

Key Details

  • Hexadecimal Display: Using %02X in printf ensures bytes are shown as two-digit hex values (e.g., 0x0A instead of 0xA), which is standard for binary file analysis.
  • Large File Optimization: If your .gfx files are extremely large, replace Files.readAllBytes() with FileInputStream to read bytes incrementally (avoids loading the entire file into memory).
  • Contextual Analysis: Once you have the differing positions, use a hex editor (like HxD or Hex Fiend) to inspect those locations—you’ll likely find differences in metadata (export timestamps, software version flags) rather than actual graphic content, since the visuals appear identical.

Optional Enhancements

  • Limit Output: If there are hundreds of differences, add a cap to print only the first 50 (avoids console clutter).
  • Export to File: Write differences to a text file using PrintWriter for later reference.
  • Surrounding Context: For each differing byte, print 5-10 surrounding bytes to better understand the affected section of the file.

内容的提问来源于stack exchange,提问作者Rahul Prajapati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:20:31