Java文本文件比较-过量和缺失
我需要比较两个文本文件(Java文本文件比较-过量和缺失,java,file,comparison,Java,File,Comparison,我需要比较两个文本文件(MasterCopy.txt和ClientCopy.txt)。我想获得ClientCopy.txt中缺少的字符串列表。还需要获取多余字符串的列表 MasterCopy.txt的内容 伦敦 巴黎 罗马 ClientCopy.txt的内容 伦敦 柏林 罗马 阿姆斯特丹 我想得到这些结果 缺失: 巴黎 超额: 柏林 阿姆斯特丹 想到的两个想法是获得两个文件的差异: 从他们的维基 任务1:计算到文件之间的差异并打印其增量 解决方案: import difflib.
MasterCopy.txt
和ClientCopy.txt)
。我想获得ClientCopy.txt中缺少的字符串列表。还需要获取多余字符串的列表
MasterCopy.txt的内容
- 伦敦
- 巴黎
- 罗马
- 伦敦
- 柏林
- 罗马
- 阿姆斯特丹
- 巴黎
- 柏林
- 阿姆斯特丹
import difflib.*;
public class BasicJavaApp_Task1 {
// Helper method for get the file content
private static List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public static void main(String[] args) {
List<String> original = fileToLines("originalFile.txt");
List<String> revised = fileToLines("revisedFile.xt");
// Compute diff. Get the Patch object. Patch is the container for computed deltas.
Patch patch = DiffUtils.diff(original, revised);
for (Delta delta: patch.getDeltas()) {
System.out.println(delta);
}
}
}
导入difflib.*;
公共类基本应用程序任务1{
//获取文件内容的助手方法
私有静态列表文件线(字符串文件名){
列表行=新建LinkedList();
字符串行=”;
试一试{
BufferedReader in=新的BufferedReader(新文件读取器(文件名));
而((line=in.readLine())!=null){
行。添加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
回流线;
}
公共静态void main(字符串[]args){
List original=filetoline(“originalFile.txt”);
List REVIDED=fileToLines(“revisedFile.xt”);
//计算差异。获取面片对象。面片是计算增量的容器。
Patch Patch=DiffUtils.diff(原件,修订版);
对于(增量增量:patch.getDeltas()){
系统输出打印项数(增量);
}
}
}
或使用哈希集:
修改@Nic的答案以使用哈希集:
Scanner s = new Scanner(new File(“MasterCopy.txt”));
HashSet<String> masterlist = new HashSet<String>();
while (s.hasNext()){
masterlist.put(s.next());
}
s.close();
s = new Scanner(new File(“ClientCopy.txt”));
HashSet<String> clientlist = new HashSet<String>();
while (s.hasNext()){
clientlist.put(s.next());
}
s.close();
//Do the comparison
ArrayList<String> missing = new ArrayList<String>();
ArrayList<String> excess = new ArrayList<String>();
//Check for missing or excess
for(String line : masterlist){
if(clientlist.get(line) == null) missing.add(line);
}
for(String line : clientlist){
if(masterlist.get(line) == null) excess.add(line);
}
Scanner s=新扫描仪(新文件(“MasterCopy.txt”);
HashSet masterlist=新HashSet();
而(s.hasNext()){
主列表。放置(s.next());
}
s、 close();
s=新扫描仪(新文件(“ClientCopy.txt”);
HashSet clientlist=新HashSet();
而(s.hasNext()){
put(s.next());
}
s、 close();
//做比较
ArrayList missing=新的ArrayList();
ArrayList过剩=新的ArrayList();
//检查是否缺失或过量
用于(字符串行:主列表){
如果缺少(clientlist.get(line)==null),则添加(line);
}
for(字符串行:clientlist){
if(masterlist.get(line)==null)多余。添加(line);
}
如果执行时间不是一个很大的因素,您可以这样做,假设您只比较每一行:
//Get the files into lists
Scanner s = new Scanner(new File(“MasterCopy.txt”));
HashSet<String> masterlist = new HashSet<String>();
while (s.hasNext()){
masterlist.add(s.next());
}
s.close();
s = new Scanner(new File(“ClientCopy.txt”));
HashSet<String> clientlist = new HashSet<String>();
while (s.hasNext()){
clientlist.add(s.next());
}
s.close();
//Do the comparison
HashSet<String> missing = new HashSet<String>();
HashSet<String> excess = new HashSet<String>();
//Check for missing or excess
for(String s : masterlist){
if(!clientlist.contains(s)) missing.add(s);
}
for(String s : clientlist){
if(!masterlist.contains(s)) excess.add(s);
}
//将文件放入列表中
扫描仪s=新扫描仪(新文件(“MasterCopy.txt”);
HashSet masterlist=新HashSet();
而(s.hasNext()){
添加(s.next());
}
s、 close();
s=新扫描仪(新文件(“ClientCopy.txt”);
HashSet clientlist=新HashSet();
而(s.hasNext()){
添加(s.next());
}
s、 close();
//做比较
HashSet missing=新HashSet();
HashSet过剩=新HashSet();
//检查是否缺失或过量
用于(字符串s:主列表){
如果(!clientlist.contains)缺少,请添加;
}
for(字符串s:clientlist){
如果(!masterlist.包含多个),则添加多个;
}
您的文件有多大?您已经尝试过什么?这个问题,以其当前形式,不适合堆栈溢出。所以主要是为了帮助调试现有代码。这个问题要求用户有效地为您编写代码。到目前为止你试过什么?什么还不起作用?发布您的一些代码,有人可以帮助您调试和修复它。我认为HashSet将是最有益的,因为字符串的查找时间是O(1),并且数组的内存大小基于hash函数和元素数,不是所存储字符串的长度。请编辑以使用哈希集。@Zoro如果有效,请将其标记为所选答案。:)