方式一:
通过比较文件每一个字节判断
public static boolean isSameFile(String filePath1, String filePath2) {
FileInputStream fis1 = null;
FileInputStream fis2 = null;
try {
fis1 = new FileInputStream(filePath1);
fis2 = new FileInputStream(filePath2);
// 获取文件的总字节数
int len1 = fis1.available();
int len2 = fis2.available();
// 判断两个文件的字节长度是否一样,长度相同则比较具体内容
if (len1 == len2) {
// 建立字节缓冲区
byte[] data1 = new byte[len1];
byte[] data2 = new byte[len2];
// 将文件写入缓冲区
fis1.read(data1);
fis2.read(data2);
// 依次比较文件中的每个字节
for (int i = 0; i < len1; i++) {
if (data1[i] != data2[i]) {
System.out.println("文件内容不一样");
return false;
}
}
System.out.println("文件内容相同");
return true;
} else {
// 文件长度不一样,内容肯定不同
System.out.println("文件内容不同");
return false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
// 关闭资源
if (fis1!=null){
try {
fis1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis2!=null){
try {
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
改进,使用缓冲流比较,在比较大文件时效率相比普通流效率高
public static boolean isSameFile2(String filePath1, String filePath2) {
BufferedInputStream bis1 = null;
BufferedInputStream bis2 = null;
FileInputStream fis1 = null;
FileInputStream fis2 = null;
try {
// 获取文件输入流
fis1 = new FileInputStream(filePath1);
fis2 = new FileInputStream(filePath2);
// 将文件输入流包装成缓冲流
bis1 = new BufferedInputStream(fis1);
bis2 = new BufferedInputStream(fis2);
// 获取文件字节总数
int len1 = bis1.available();
int len2 = bis2.available();
// 判断两个文件的字节长度是否一样,长度相同则比较具体内容
if (len1 == len2) {
// 建立字节缓冲区
byte[] data1 = new byte[len1];
byte[] data2 = new byte[len2];
// 将文件写入缓冲区
bis1.read(data1);
bis2.read(data2);
// 依次比较文件中的每个字节
for (int i = 0; i < len1; i++) {
if (data1[i] != data2[i]) {
System.out.println("文件内容不一致");
return false;
}
}
System.out.println("文件内容一致");
return true;
} else {
System.out.println("文件长度不一致,内容不一致");
return false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis1 != null) {
try {
bis1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis2 != null) {
try {
bis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis1 != null) {
try {
fis1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis2 != null) {
try {
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
对比测试和测试结果
public static void main(String[] args) throws IOException {
String filePath1 = "/Users/zhouzhxu/desktop/test.csv";
String filePath2 = "/Users/zhouzhxu/desktop/test2.csv";
long start1 = System.currentTimeMillis();
System.out.println("isSameFile : " + isSameFile(filePath1, filePath2));
long end1 = System.currentTimeMillis();
long time1 = end1 - start1;
System.out.println("isSameFile 耗时 : " + time1);
long start2 = System.currentTimeMillis();
System.out.println("isSameFile2 : " + isSameFile2(filePath1, filePath2));
long end2 = System.currentTimeMillis();
long time2 = end2 - start2;
System.out.println("isSameFile2 耗时 : " + time2);
}
方式二:
将文件分多次读入,然后通过MessageDigest进行MD5加密,最后再通过BigInteger类提供的方法进行16进制的转换
/**
* 计算文件的MD5值
*
* @param file
* @return
*/
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte[] buffer = new byte[8192];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
String filePath1="/Users/zhouzhxu/desktop/test.csv";
String filePath2="/Users/zhouzhxu/desktop/test2.csv";
String fileMD51 = getFileMD5(new File(filePath1));
String fileMD52 = getFileMD5(new File(filePath2));
System.out.println(fileMD51);
System.out.println(fileMD52);
}
方式三:
使用apache下的commons-codec包
public static void main(String[] args) throws IOException {
String filePath1="/Users/zhouzhxu/desktop/test.csv";
String filePath2="/Users/zhouzhxu/desktop/test2.csv";
String md5Hex1 = DigestUtils.md5Hex(new FileInputStream(filePath1));
String md5Hex2 = DigestUtils.md5Hex(new FileInputStream(filePath1));
System.out.println(md5Hex1);
System.out.println(md5Hex2);
}