-1

基本上,我有两个完全限定文件名的字符串。我想比较这两个文件是一样的。所以我将两个字符串都转换为文件对象。使用谷歌的 Files.equal(File file, File file2) 方法,我试图比较它们,但返回的值是假的。但是,想知道出了什么问题,我将两个文件对象都转换为字节数组并输出那些等于相同数字的对象。那么,有谁知道为什么 Files.equal 认为它们是错误的。

我只是好奇为什么该方法返回false,因为在阅读文档后 Files.equal 按字节比较两个文件。

谢谢。

代码:

    public class WhenEncrypting {

private String[] args = new String[4];

/**
 * encrypts a plain text file
 * 
 * @throws IOException
 *             IOException could occur
 */
@Test()
public void normalEncryption() throws IOException {
    this.args[0] = "-e";
    this.args[1] = "./src/decoderwheel/tests/valid.map";
    this.args[2] = "./src/decoderwheel/tests/input.txt";
    this.args[3] = "./src/decoderwheel/tests/crypt.txt";

    DecoderWheel.main(this.args);

    File plainFile = new File("./src/decoderwheel/tests/input.txt");
    File crypted = new File("./src/decoderwheel/tests/crypt.txt");

    byte[] f1 = Files.toByteArray(plainFile);
    byte[] f2 = Files.toByteArray(crypted);
    int number = f1.length;
    int size = f2.length;
    Files.equal(crypted, plainFile);
    System.out.println(number);
    System.out.println(size);
    System.out.println(Files.equal(crypted, plainFile));

    assertTrue(Files.equal(crypted, plainFile));

}

}

 Output:
 360
 360
 false
4

1 回答 1

1

根据您向我们展示的内容,我认为问题很可能是两个文件的内容不相等。

两个字节数组(从文件中读取)具有相同长度的事实并不意味着它们的内容(以及因此文件的内容)是相同的。

添加如下内容:

for (int i = 0; i < f1.length; i++) {
    if (f1[i] != f2[i]) {
        System.out.println("File content mismatch at index " + i + ": " + 
                           f1[i] + " != " + f2[i]);
    }
}
于 2013-10-20T01:28:14.663 回答