判断两个文件内容是否相同
FileInputStream
public class Test01 {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream1 = new FileInputStream("F:\\a\\copy.txt");
FileInputStream fileInputStream2 = new FileInputStream("F:\\a\\copy.jpg");
//返回中的字节数
int len1 = fileInputStream1.available();//用file.length()方法也可以
int len2 = fileInputStream2.available();
System.out.println(len1);
System.out.println(len2);
if(len1 == len2) {//长度比较,长度相同就比较具体内容
//建立两个字节缓冲区
//字节数组是为了存入数据
byte[] b1 = new byte[len1];
byte[] b2 = new byte[len2];
//将两个文件读入缓冲区
fileInputStream1.read(b1);
fileInputStream2.read(b2);
//依次比较文件中的每个字节
for(int i =0;i<len1;i++) {
if (b1[i] != b2[i]) {
System.out.println("文件内容不一样");
}
}
System.out.println("文件一样");
}else {
System.out.println("文件长度不一样,文件不一样");
}
fileInputStream1.close();
fileInputStream2.close();
}
}