首先我创建签名(字节[]签名)假。然后我将这个签名写入文件。我从这个文件读取签名并给它另一个变量(byte [] signatureCopy)。但是当我比较这两个变量时,它返回false。我该如何解决它?阵列等于返回相同的字节数组
但是,当我打印屏幕,它的外观一样。
System.out.println (new String (signature));
System.out.println (new String (signatureCopy));
代码:
byte[] signature = this.signature(data);
FileUtil.writeRegistryFileSigned(path, signature);
byte[] signatureCopy = FileUtil.readSignatureInRegistryFile(path);
System.out.println(Arrays.equals(signature, signatureCopy)); //FALSE
功能;
public static byte[] readSignatureInRegistryFile(String filePath){
byte[] data = null;
try {
data = Files.readAllBytes(Paths.get(filePath));
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static void writeRegistryFileSigned(String filePath, byte[] signature) {
File fileRegistry = new File(filePath);
try {
fileRegistry.createNewFile();
} catch (IOException e1) {
}
try (FileWriter fw = new FileWriter(fileRegistry, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(new String(signature));
} catch (IOException e) {
}
}
请勿使用Writer写入二进制数据。使用OutputStream。新字符串(签名)是一种有损操作,因为字节数组不代表使用默认字符集编码的字符,并且println()会添加EOL字符。 –
谢谢。 OutputStream工作。为什么Writer不工作,OutputStream的作品。你可以解释吗? – cezaalp
我已经拥有。重新阅读我的评论。 –