复制验证文件是否在Java中复制
我正在将一些文件移动到我项目中的另一个目录中,效果很好,只是我无法验证它是否被正确移动
我想验证副本的长度是否与原件相同,然后我想删除原件。我在进行验证之前关闭了两个文件流,但仍然失败,因为大小不同。下面是我关闭流、验证和删除的代码
in.close();
out.close();
if (encCopyFile.exists() && encCopyFile.length() == encryptedFile.length())
encryptedFile.delete();
在此之前的其余代码都是使用Util来复制流,而且一切正常,所以我真的需要一种更好的验证方法
# 1 楼答案
检查的一个好方法是比较md5哈希。检查文件长度并不意味着它们是相同的。虽然md5哈希并不意味着它们是相同的,但它比检查长度要好,尽管这是一个较长的过程
# 2 楼答案
如果大小不同,可能在关闭输出流之前没有刷新它
哪个文件更大?每个文件的大小是多少?你真的看过这两个文件,看看有什么不同吗
# 3 楼答案
您可以使用commons io:
或者可以使用校验和方法: