java分别读取两个txt文件里的数据再进行比较。再分别列出这两个文件中共有的和分别独有的数据。 30

两个txt文件里存着不同数量的推文数据,每一行开头的一串数字是该条推文的ID,ID和推文内容用“:”冒号隔开,根据ID比较两个txt文件中推文,再分别列出连个txt文件中... 两个txt文件里存着不同数量的推文数据,每一行开头的一串数字是该条推文的ID,ID和推文内容用“:”冒号隔开,根据ID比较两个txt文件中推文,再分别列出连个txt文件中共有的推文,第一个文件独有的,和第二个文件独有的推文。
txt文件如下。求份详细点的代码。
1.txt
232204745903841280:RT @ZOO: Amazing - a runner falls over in the women''s 400m hurdles, but look at her surname. You couldn''t write it #London2012 http://t. ...
232204745455046658:RT @mrianleslie: Why is that our Olympic athletes are so impressive and likeable in interviews and our footballers so dull, dim and char ...
232204746340044800:They should have Olympic breaks to give you a chance to run to the fridge to get the beer !
232204746574946306:He nearly got knocked out and took the fight hahaha #nevin #London2012
2.txt
232204746340044800:They should have Olympic breaks to give you a chance to run to the fridge to get the beer !
232204746574946306:He nearly got knocked out and took the fight hahaha #nevin #London2012
232204745958371328:RT @ZOO: Amazing - a runner falls over in the women''s 400m hurdles, but look at her surname. You couldn''t write it #London2012 http://t. ...
232204746281320449:RT @mikelgustafson: Katie Ledecky cannot: drive, vote, drink, smoke, gamble, buy a handgun, or attend "Magic MIke"... but she can become ...
232204746830786560:Marik que nervios Katerin Ibarguen!!.. @London2012
展开
 我来答
willamsccd
2013-03-06 · TA获得超过118个赞
知道小有建树答主
回答量:154
采纳率:0%
帮助的人:87.1万
展开全部
ID后面是以个推文还是多个推文?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class FileCompare {
public static void main(String[] args) throws IOException {
FileCompare c = new FileCompare();
c.compareFile();
}

public void compareFile(){
File file = new File("D:\\1.txt");
File file2 = new File("D:\\2.txt");
Set fileTextSet = new HashSet();
Set file2TextSet = new HashSet();
try {
getText(file,fileTextSet);
getText(file2,file2TextSet);
compareSet(fileTextSet, file2TextSet);
} catch (Exception e) {
e.printStackTrace();
}
}

private void compareSet(Set textSet,Set StextSet2){
for (Iterator iterator = textSet.iterator(); iterator.hasNext();) {
String name = (String) iterator.next();
if(StextSet2.contains(name)){
System.out.println("共同报文:"+name);
}else{
System.out.println("1独有报文:"+name);
}
}
for (Iterator iterator = StextSet2.iterator(); iterator.hasNext();) {
String name = (String) iterator.next();
if(!textSet.contains(name)){
System.out.println("2独有报文:"+name);
}
}
}
private void getText(File file,Set textSet) throws IOException {
BufferedReader br = null;
InputStream is = null;
try {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String lineStr = null;
while ((lineStr = br.readLine())!=null) {
String text = lineStr.substring(lineStr.indexOf(":"));//按照需求切分
textSet.add(text);
}
if(br!=null){
br.close();
}
if(is!=null){
is.close();
}
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
} finally {
if(br!=null){
br.close();
}
if(is!=null){
is.close();
}
}
}

}
追问

怎样改代码可以在打印结果里分别显示共同报文、1独有报文和2独有报文呢?现在的是区分出报文后分一条写一条,三种报文混在一起的。如图。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
TableDI
2024-07-18 广告
在上海悉息信息科技有限公司,我们深知Excel在数据处理中的重要作用。在Excel中引用不同工作表(sheet)的数据是常见的操作,这有助于整合和分析跨多个工作表的信息。通过在工作表名称前加上感叹号“!”,您可以轻松地引用其他工作表中的数据... 点击进入详情页
本回答由TableDI提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式