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

解决方案 »

  1.   

    用inputstring读入文本的内容到两个STRING
    再用STRING。equals比较
      

  2.   

    我写了一个比较粗的程序。大家帮我改改。import java.io.*;
    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();
        }
      }
    }
      

  3.   

    会的,可以借助其他工具,如unix下的什么冬冬(我忘了)
      

  4.   

    我不知道有什么现成的方法,不过可以肯定的是:如果要判定两个文件完全相同只有把所有字节匹配了以后才行,所以匹配的重点应该在如何快速的判定两个文件不完全相同,这里就是一个算法研究的问题了,应该会有一个比较成型的算法,不过肯定不会是逐行匹配最优。初步想了一个方法是将文件分为n段,依次匹配每段的第1,2...个字符。即先匹配第1段的第一个字符,第二段的第一个字符到第n段的第一个字符,再匹配第一段的第二个字符到第n段的第二个字符,依次类推。也可以采取从两头向中间的方式。考虑到内存占用问题,可以改为每次读入一段匹配一段,即先匹配第一段再匹配第n段,从两头向中间。每段的大小又有一个最优化的问题,只有试试了。