File f1 = new File(path);
f1.comparTo(File path);public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Win32 systems it is not.Parameters:
pathname - The abstract pathname to be compared to this abstract pathnameReturns:
Zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument
f1.comparTo(File path);public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Win32 systems it is not.Parameters:
pathname - The abstract pathname to be compared to this abstract pathnameReturns:
Zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument
再用STRING。equals比较
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.Collator;public class CompareFiles {
public CompareFiles() {
} public static void main(String[] args){
File file1 = new File("1.html");
File file2 = new File("2.html"); FileInputStream inFile1 = null;
FileInputStream inFile2 = null; try {
inFile1 = new FileInputStream(file1);
inFile2 = new FileInputStream(file2);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
} FileChannel inChannel1 = inFile1.getChannel();
FileChannel inChannel2 = inFile2.getChannel();
ByteBuffer buf1 = null;
ByteBuffer buf2 = null;
try {
buf1 = ByteBuffer.allocate((int)inChannel1.size());
buf2 = ByteBuffer.allocate((int)inChannel2.size());
}
catch (IOException ex2) {
ex2.printStackTrace();
} String result1=null;
String result2=null;
try { while (inChannel1.read(buf1) != -1) {
result1 = new String(buf1.array(),"gb2312");
buf1.clear();
}
while (inChannel2.read(buf2) != -1) {
result2 = new String(buf2.array(),"gb2312");
buf2.clear();
}
if(result1.equals(result2)){
System.out.println("same");
}
else{
System.out.println("diff");
} inFile1.close();
inFile2.close();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
}
}