本帖最后由 java2000_net 于 2008-08-04 21:21:02 编辑

解决方案 »

  1.   

    文本比较可以考虑使用diff,java实现的有java-diff
    一般是实现最长子串算法 
      

  2.   

    textdiff,应该能满足你的要求。
    不过我也没用过,哈哈。一般我都用merge6.5 或者beyond compare2
      

  3.   

    最简单的方法。把2个文件都复制到eclipse下面。
    然后选中两个文件(选中一个,再按住CTRL,选中另一个),点击右键,选“compare with -> each other”。
      

  4.   

    我要的不是这样的,
    我要的是比较文本内容,我写的程序里需要一个组件去实现这个功能,所以我想要一个类似java-diff的jar包,但是我想比较的是内容,就是说无论文件里的行的顺序如何,我们都认为它是相同的,而不是按行比较,谁知道,谢谢了
      

  5.   

    读xml文件是按元素和节点读的。
    比较元素和节点应该就可以了。
    不知道对不对~
      

  6.   

    把你的文件转换成String 然后分别replaceAll(System.getProperty("line.separator")); 
    然后再比较就可以了
      

  7.   

    给你一个比较文件的方法吧:
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;public class DirSyncCompare {

    /**
     * @param srcPath
     * @param destPath
     * @return "ok": 表示完全一样;如果没有',':文件类型不匹配;有',',不同的文件列表
     */
    public static final String dirCompare(String srcPath,String destPath){
    File srcFile = new File(srcPath);
    File tagFile = new File(destPath);
    String result = comparate(srcFile, tagFile);
    if("".equals(result))
    return "ok";
    else if(result.lastIndexOf("\r\n")>-1)
    return result.substring(0,result.lastIndexOf("\r\n"));
    else
    return result;
    }


    /**
     * 比较两个文件是否相等,并包含文件目录夹下子目录下面的文件
     * @param srcFile 源文件地址
     * @param tagFile 目标文件地址
     * @return 一样返回为""
     */
    public static final String comparate(File srcFile,File tagFile)  
    { boolean equals = 
    (srcFile.isDirectory() && tagFile.isDirectory()) || ( srcFile.isFile() && tagFile.isFile());   
    if(!equals){          
       return srcFile.getAbsolutePath() + "与" + tagFile.getAbsolutePath() + "类型不一致\r\n";
    }
    if(srcFile.isFile()){
      try {
      equals = equals(srcFile,tagFile);
      if(equals){
      return "";
      }
      else return srcFile.getName()+"\r\n";
      } 
      catch (IOException e) {
      return "比较文件" + srcFile.getAbsolutePath() + "与" + tagFile.getAbsolutePath() + "IO ERROR\r\n";
      }
    }
    else {
        File[] srcFiles = srcFile.listFiles();
        File[] tagFiles = tagFile.listFiles();
        Map<String,File> maps = new HashMap<String,File>();
    StringBuffer buffer = new StringBuffer();
    for(int i = 0 ; tagFiles != null && i < tagFiles.length; i++){
    maps.put(tagFiles[i].getName().toLowerCase(),tagFiles[i]);
    }
    for(int i = 0 ; srcFiles != null && i < srcFiles.length; i++){
    String key = srcFiles[i].getName().toLowerCase();
    File srcChildFile = srcFiles[i];
    File tagChildFile = maps.get(key);
    /*String result = comparate(srcChildFile,tagChildFile);
        if(tagChildFile != null&&result!=null)
         buffer.append(comparate(srcChildFile,tagChildFile));*/
    if(tagChildFile != null)
         buffer.append(comparate(srcChildFile,tagChildFile));
    else {
    buffer.append(srcChildFile.getName() + " miss\r\n!");
    }
    }
    /*String result = buffer.toString().equals("")? "ok":buffer.toString();
    if(result.lastIndexOf(",")>-1)
    result = result.substring(0,result.lastIndexOf(","));
    return result;*/
    return buffer.toString();
    }
        }


    public static final boolean equals(File file1,File file2) throws IOException{
      long size1 = file1.length();
      long size2 = file2.length();
      //如果文件大小不一致,不相等退出
      if(size1 != size2) 
      return false;
      //如果文件名不一致,表示不相等
      if(!file1.getName().equals(file2.getName())){
      return false;
        }
      if(size1 == 0) return true;
      BufferedInputStream buf1 = null;
      BufferedInputStream buf2 = null;
         try
      {
         buf1 = new BufferedInputStream(new FileInputStream(file1));
         buf2 = new BufferedInputStream(new FileInputStream(file2));
     long bufferSize = size1 > 4096 ? 4096 : size1;
     byte[] bytes1 = new byte[(int) bufferSize];
     byte[] bytes2 = new byte[(int) bufferSize];
     int end = 0;
     while((end = buf1.read(bytes1)) != -1 && (end = buf2.read(bytes2)) != -1){
    for(int i=0; i<end; i++){
    if(bytes1[i] != bytes2[i])
    {
    return false;
    }
    }
        }
            return true;
      }
      finally{
         if(buf1 != null) {
     try{
     buf1.close();
     }
     catch(IOException ex){}
         }
     if(buf2 != null){
      try{
      buf2.close();
      }
      catch(IOException ex){
      buf2.close();
      }
     }
      }
      
      }
    }