对比两个txt大文件,写出差异值

该程序能对比两个大txt文件,已测两个400M文件对比,结果在三分钟内执行完毕,需在Linux下执行,Windows下容易出现内存不足。

package test;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReadTxtUtils {
 public static void main(String args[]) {
  readAndWriteFile();
 }
 /**
  * 
  * 读入TXT文件
  * 
  */
 public static void readAndWriteFile() {
   String pathname = "E:/workTRS/vdb/vdb/10.78.1.94_VDB.trs";
   String pathname2 = "E:/workTRS/vdb/vdb/10.78.1.69_VDB.trs";
//  String pathname = "/home/test1/10.78.1.94_VDB.trs";
//  String pathname2 = "/home/test1/10.78.1.69_VDB.trs";
  List<String> List1 = new ArrayList<String>();
  List<String> List2 = new ArrayList<String>();
  Map<String, String> map = new HashMap<String, String>();
  try (
    FileReader reader = new FileReader(pathname); // 建立一个对象,它把文件内容转成计算机能读懂的语言
    BufferedReader br = new BufferedReader(reader);
    FileReader reader2 = new FileReader(pathname2); // 建立一个对象,它把文件内容转成计算机能读懂的语言
    BufferedReader br2 = new BufferedReader(reader2);
  ) {
   String line;
   while ((line = br.readLine()) != null) {
    List1.add(line);
   }
   while ((line = br2.readLine()) != null) {
    List2.add(line);
   }
   System.out.println(List1);
   System.out.println(List2);
   // 下面是写入文件
   File writeName = new File("E:/workTRS/vdb/vdb/output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
   writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
   try (FileWriter writer = new FileWriter(writeName);
     BufferedWriter out = new BufferedWriter(writer)
   ) {
    out.write(ReadTxtUtils.getDiffrent4(List1, List2) + ""); // \r\n即为换
    out.flush(); // 把缓存区内容压入文件
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 /**
  * 
  * 写入TXT文件
  * 
  */
 public static void writeFile() {
  try {
   File writeName = new File("/home/test1/output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
   writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
   try (FileWriter writer = new FileWriter(writeName);
     BufferedWriter out = new BufferedWriter(writer)
   ) {
    out.write("88888\r\n"); // \r\n即为换行
    out.flush(); // 把缓存区内容压入文件
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public static List<String> getDiffrent4(List<String> list1, List<String> list2) {
  Map<String, Integer> map = new HashMap<String, Integer>(list1.size() + list2.size());
  List<String> diff = new ArrayList<String>();
  List<String> maxList = list1;
  List<String> minList = list2;
  if (list2.size() > list1.size()) {
   maxList = list2;
   minList = list1;
  }
  for (String string : maxList) {
   map.put(string, 1);
  }
  for (String string : minList) {
   Integer cc = map.get(string);
   if (cc != null) {
    map.put(string, ++cc);
    continue;
   }
   map.put(string, 1);
  }
  for (Map.Entry<String, Integer> entry : map.entrySet()) {
   if (entry.getValue() == 1) {
    diff.add(entry.getKey());
   }
  }
  return diff;
 }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的Pandas库来实现这一步骤:1. 使用Pandas中的read_excel()函数读取两个Excel文件。 2. 使用Pandas中的merge()函数将这两个文件合并。 3. 使用Pandas中的drop_duplicates()函数删除重复的。 4. 使用Pandas中的to_excel()函数将剩余的数据存为两个新的Excel文件。 ### 回答2: 在Python中,可以使用`pandas`库来进Excel文件的读取、处理和保存。下面是一个示例代码,用于将两个Excel文件对比,删除相同数据,并将剩下的数据分别保存到两个文件中。 首先,需要安装`pandas`库: ```python pip install pandas ``` 然后,通过以下代码实现对比和分割保存的功能: ```python import pandas as pd # 读取两个Excel文件数据 df1 = pd.read_excel('file1.xlsx') df2 = pd.read_excel('file2.xlsx') # 对比两个文件数据差异,并保留不同数据 df_diff1 = pd.DataFrame(df1).merge(df2, how='left', indicator=True) df_diff1 = df_diff1[df_diff1['_merge'] == 'left_only'] df_diff2 = pd.DataFrame(df2).merge(df1, how='left', indicator=True) df_diff2 = df_diff2[df_diff2['_merge'] == 'left_only'] # 删除'_merge'列 df_diff1 = df_diff1.drop(columns='_merge') df_diff2 = df_diff2.drop(columns='_merge') # 保存差异数据两个文件 df_diff1.to_excel('file1_diff.xlsx', index=False) df_diff2.to_excel('file2_diff.xlsx', index=False) ``` 以上代码首先使用`pd.read_excel()`函数读取两个Excel文件数据,并存储在DataFrame对象中。接下来,使用`merge`函数将两个DataFrame对象进对比,并使用`'_merge'`列指示合并的结果。将合并结果为`'left_only'`的保留,即为两个文件不同的数据。然后,使用`drop`函数删除`'_merge'`列,并使用`to_excel`函数将结果保存为新的Excel文件。 请注意,以上代码假设两个Excel文件中的数据结构和列名是相同的。如果两个文件数据结构不同,需要根据实际情况进相应的处理。 ### 回答3: 您可以使用Python的`pandas`库来实现这个功能。下面是一个示例代码: ```python import pandas as pd # 读取两个Excel文件数据框 df1 = pd.read_excel("file1.xlsx") df2 = pd.read_excel("file2.xlsx") # 将两个数据框合并,使用indicator参数记录数据来自哪个文件 merged = pd.concat([df1, df2], ignore_index=True, sort=False, keys=["file1", "file2"], names=["File"]) merged["_merge"] = merged.duplicated(keep=False) # 根据数据来自的文件和标记连接方式,判断数据是否相同 merged["is_duplicate"] = merged["_merge"].map({'left_only': False, 'right_only': False, 'both': True}) # 过滤出不相同的 filtered = merged[merged["is_duplicate"] == False].copy() # 删除多余的列 filtered.drop(["_merge", "is_duplicate"], axis=1, inplace=True) # 将数据拆分为两个数据框 df1_new = filtered[filtered.index.get_level_values("File") == "file1"].droplevel("File") df2_new = filtered[filtered.index.get_level_values("File") == "file2"].droplevel("File") # 将两个数据框另存为新的Excel文件 df1_new.to_excel("file1_new.xlsx", index=False) df2_new.to_excel("file2_new.xlsx", index=False) ``` 这段代码首先使用`pandas`的`read_excel`函数读取两个Excel文件数据框。然后将两个数据框合并为一个新的数据框,并添加一个`_merge`列来标记数据来自哪个文件。接下来根据标记判断数据是否相同,过滤出不相同的,并删除多余的列。最后将两个数据框分别另存为新的Excel文件。 请根据您的实际情况修改文件名和路径,并确保已安装好`pandas`库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值