总是只是从每个文件中逐字节读取并随时比较它们.Md5和Sha1等仍然必须读取所有字节,因此计算哈希是额外的工作,你不必这样做.
if(file1.length() != file2.length()){
return false;
}
try( InputStream in1 =new BufferedInputStream(new FileInputStream(file1));
InputStream in2 =new BufferedInputStream(new FileInputStream(file2));
){
int value1,value2;
do{
//since we're buffered read() isn't expensive
value1 = in1.read();
value2 = in2.read();
if(value1 !=value2){
return false;
}
}while(value1 >=0);
//since we already checked that the file sizes are equal
//if we're here we reached the end of both files without a mismatch
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9142 次 |
| 最近记录: |