Saturday, 21 June 2014

Check Two files are equal or not in java

                 // TODO Auto-generated method stub

File file1 = new File("E:\\Check\\m1.txt");
File file2 = new File("E:\\Check\\m2.txt");

FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);

StringBuffer sub1 = new StringBuffer();
StringBuffer sub2 = new StringBuffer();


int i,j;
int ch1,ch2;
if(file1.exists() && file2.exists()){
while((ch1=fis1.read()) != -1){
sub1.append((char)ch1);

}
fis1.close();
while((ch2=fis2.read()) != -1){
sub2.append((char)ch2);

}
fis2.close();
}
boolean flag = true;
String temp = null;
for(i=0,j=0; i<sub1.length();i++,j++){
if(sub1.charAt(i) != sub2.charAt(j)){
temp = (char)sub1.charAt(i)+"";
flag = false;
}

}
if(flag == false){
System.out.println("Both Files are not Same--"+temp);
}
else{
System.out.println("Both Are Same");
file2.delete();
System.out.println("Second File is SuccessFully Deleted");
}

No comments:

Post a Comment