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