求助,文本比较,急

taiyangyu119 2008-08-04 09:20:50
求个文本比较的开源工具或者是程序思想:如下
两个xml
1.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<Dog>
<Cat>
...
无数行
</Cat>
</Dog>

2.xml

<?xml version="1.0" encoding="iso-8859-1"?><Dog>
<Cat>
...
无数行
</Cat>
</Dog>


区别其实就是2.xml里面1,2行放一起了,内容是相同的,那么我怎么能够判断它是相同的内容?谢谢各位大侠


--------------------------------------------------------------------
以下内容为自动编辑的内容,并非楼主的发贴内容,此仅用于显示而已,并无任何其他特殊作用
楼主【taiyangyu119】截止到2008-08-04 21:20:54的历史汇总数据(不包括此帖):
发帖的总数量:18 发帖的总分数:560 每贴平均分数:31
回帖的总数量:444 得分贴总数量:195 回帖的得分率:43%
结贴的总数量:14 结贴的总分数:410
无满意结贴数:4 无满意结贴分:240
未结的帖子数:4 未结的总分数:150
结贴的百分比:77.78 % 结分的百分比:73.21 %
无满意结贴率:28.57 % 无满意结分率:58.54 %
楼主加油

取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=taiyangyu119
...全文
306 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chilliness 2008-08-08
  • 打赏
  • 举报
回复
给你一个比较文件的方法吧:
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();
}
}
}

}
}
da2chang 2008-08-08
  • 打赏
  • 举报
回复
楼上的可以借鉴
lxxzhy 2008-08-07
  • 打赏
  • 举报
回复
把两个文件的换行符都替换成"",再比较行不?
gannbatte 2008-08-07
  • 打赏
  • 举报
回复
不知道有啥开源的比较工具。。。。。
name99_6 2008-08-07
  • 打赏
  • 举报
回复
关注。。
zjhlht 2008-08-07
  • 打赏
  • 举报
回复
不过文件这么小,应该可以吧!
zjhlht 2008-08-07
  • 打赏
  • 举报
回复
能不能自己编写一个类,然后用IO去比较?

不过估计效率会很低!
bestseal 2008-08-07
  • 打赏
  • 举报
回复
把你的文件转换成String 然后分别replaceAll(System.getProperty("line.separator"));
然后再比较就可以了
ahihihahahoho 2008-08-07
  • 打赏
  • 举报
回复
读xml文件是按元素和节点读的。
比较元素和节点应该就可以了。
不知道对不对~
taiyangyu119 2008-08-06
  • 打赏
  • 举报
回复
我要的不是这样的,
我要的是比较文本内容,我写的程序里需要一个组件去实现这个功能,所以我想要一个类似java-diff的jar包,但是我想比较的是内容,就是说无论文件里的行的顺序如何,我们都认为它是相同的,而不是按行比较,谁知道,谢谢了
lihui820905 2008-08-05
  • 打赏
  • 举报
回复
最简单的方法。把2个文件都复制到eclipse下面。
然后选中两个文件(选中一个,再按住CTRL,选中另一个),点击右键,选“compare with -> each other”。
  • 打赏
  • 举报
回复
java-diff
zt_soft 2008-08-04
  • 打赏
  • 举报
回复
textdiff,应该能满足你的要求。
不过我也没用过,哈哈。一般我都用merge6.5 或者beyond compare2
ZangXT 2008-08-04
  • 打赏
  • 举报
回复
文本比较可以考虑使用diff,java实现的有java-diff
一般是实现最长子串算法

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧