基本上,我有两个完全限定文件名的字符串。我想比较这两个文件是一样的。所以我将两个字符串都转换为文件对象。使用谷歌的 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