Java 比较两个文件中的值
我有两个文件,它们应该在子字符串0和10之间包含相同的值,尽管顺序不同。我已经成功地输出了每个文件中的值,但我需要知道如何报告id,比如说值在第一个文件中,而不是在第二个文件中,反之亦然。这些文件采用这些格式Java 比较两个文件中的值,java,bufferedreader,Java,Bufferedreader,我有两个文件,它们应该在子字符串0和10之间包含相同的值,尽管顺序不同。我已经成功地输出了每个文件中的值,但我需要知道如何报告id,比如说值在第一个文件中,而不是在第二个文件中,反之亦然。这些文件采用这些格式 6436346346....Other details 9348734873....Other details 9349839829....Other details 第二档 8484545487....Other details 9348734873....Other details
6436346346....Other details
9348734873....Other details
9349839829....Other details
第二档
8484545487....Other details
9348734873....Other details
9349839829....Other details
第一个文件中的第一条记录不会出现在第二个文件中,第二个文件中的第一条记录也不会出现在第一个文件中。我需要能够以以下格式报告此不匹配:
Record 6436346346 is in the firstfile and not in the secondfile.
Record 8484545487 is in the secondfile and not in the firstfile.
下面是我目前拥有的代码,它为我提供了两个文件中需要比较的输出
package compare.numbers;
import java.io.*;
/**
*
* @author implvcb
*/
public class CompareNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
File f = new File("C:/Analysis/");
String line;
String line1;
try {
String firstfile = "C:/Analysis/RL001.TXT";
FileInputStream fs = new FileInputStream(firstfile);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
while ((line = br.readLine()) != null) {
String account = line.substring(0, 10);
System.out.println(account);
}
String secondfile = "C:/Analysis/RL003.TXT";
FileInputStream fs1 = new FileInputStream(secondfile);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fs1));
while ((line1 = br1.readLine()) != null) {
String account1 = line1.substring(0, 10);
System.out.println(account1);
}
} catch (Exception e) {
e.fillInStackTrace();
}
}
}
请帮助我如何有效地实现这一点。
我想我需要说的是,我是java新手,可能不太容易理解,但我正在尝试。如果您保证文件的格式始终相同,并且每个readLine()函数将返回不同的数字,为什么不使用字符串数组,而不是单个字符串呢。然后,您可以更轻松地比较结果。以下是执行此操作的示例代码:
public static void eliminateCommon(String file1, String file2) throws IOException
{
List<String> lines1 = readLines(file1);
List<String> lines2 = readLines(file2);
Iterator<String> linesItr = lines1.iterator();
while (linesItr.hasNext()) {
String checkLine = linesItr.next();
if (lines2.contains(checkLine)) {
linesItr.remove();
lines2.remove(checkLine);
}
}
//now lines1 will contain string that are not present in lines2
//now lines2 will contain string that are not present in lines1
System.out.println(lines1);
System.out.println(lines2);
}
public static List<String> readLines(String fileName) throws IOException
{
List<String> lines = new ArrayList<String>();
FileInputStream fs = new FileInputStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String line = null;
while ((line = br.readLine()) != null) {
String account = line.substring(0, 10);
lines.add(account);
}
return lines;
}
publicstaticvoideliminatecommon(stringfile1,stringfile2)抛出IOException
{
列表行1=读取行(文件1);
列表行2=读取行(文件2);
迭代器linesItr=lines1.Iterator();
while(linesItr.hasNext()){
字符串checkLine=linesItr.next();
if(行2.包含(选中行)){
linesItr.remove();
线条2.移除(勾选线);
}
}
//现在,第1行将包含第2行中不存在的字符串
//现在,第2行将包含第1行中不存在的字符串
系统输出打印LN(第1行);
系统输出打印项次(第2行);
}
公共静态列表读取行(字符串文件名)引发IOException
{
列表行=新的ArrayList();
FileInputStream fs=新的FileInputStream(文件名);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fs));
字符串行=null;
而((line=br.readLine())!=null){
字符串帐户=行。子字符串(0,10);
行。添加(帐户);
}
回流线;
}
- 将每个文件中的值相应地放入两个独立的
哈希集
- 迭代一个
s,检查另一个HashSet
中是否存在每个值。如果没有,请报告HashSet
- 迭代其他
并对其执行相同的操作HashSet
final TreeSet<Integer> ts1 = new TreeSet<Integer>();
final TreeSet<Integer> ts2 = new TreeSet<Integer>();
while (scan1.hasNextLine() && scan2.hasNexLine) {
ts1.add(Integer.valueOf(scan1.nextLigne().subString(0,10));
ts1.add(Integer.valueOf(scan1.nextLigne().subString(0,10));
}
You can now compare ordered results of the two trees
final TreeSet ts1=新TreeSet();
最终树集ts2=新树集();
while(scan1.hasNextLine()&&scan2.hasNextLine){
add(Integer.valueOf(scan1.nextLigne().subString(0,10));
add(Integer.valueOf(scan1.nextLigne().subString(0,10));
}
现在可以比较两棵树的有序结果
编辑
使用TreeSet修改,好的,首先我将两组字符串保存到集合中
Set<String> s1 = new HashSet<String>(), s2 = new HashSet<String>();
//...
while ((line = br.readLine()) != null) {
//...
s1.add(line);
}
然后您可以使用
设置。也许您正在寻找类似的功能
Set<String> set1 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL001.TXT")));
Set<String> set2 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL003.TXT")));
Set<String> onlyInSet1 = new HashSet<>(set1);
onlyInSet1.removeAll(set2);
Set<String> onlyInSet2 = new HashSet<>(set2);
onlyInSet2.removeAll(set1);
Set set1=newhashset(FileUtils.readLines(新文件(“C:/Analysis/RL001.TXT”));
Set set2=newhashset(FileUtils.readLines(新文件(“C:/Analysis/RL003.TXT”));
Set onlyInSet1=新哈希集(set1);
仅限设置1.移除所有(设置2);
Set onlyInSet2=新哈希集(set2);
仅限设置2.移除所有(设置1);
数字是按排序顺序给出的(递增还是递减)?如果不能保证这一点,这个解决方案将不起作用。(问题中的示例未排序)不,数字是以随机方式输出的,没有顺序。OP说值没有顺序。更简单的是:hashset1.removeAll(hashset2)
。剩下的所有元素都是单数。然后在另一个方向上做同样的事情(当然是新的集合)。那么,哪个参数表示文件1中的内容而不是文件1中的内容,以及文件2中的内容而不是文件1中的内容?出现错误:源代码1.6中不支持菱形分隔符onlyInSet1
=“文件1中的内容而不是文件中的内容”2您可以填写
类型。
FileUtils
来自使用集合将更有效地进行搜索。@Stanley在eliminateCommon()的末尾有注释。两个列表都包含唯一的id。您可以以自己的方式打印。System.out.println(第1行)
和System.out.println(第2行)
也不会输出任何内容。谢谢Ramesh,我想这就是我一直在寻找的,最后一件事,输出是以数组的形式出现的:[263232323236,7343476,34734343834],我可以在每一行中得到数字吗?行1会返回不在行2中的记录,但行2甚至会返回第一个文件中的记录
Set<String> set1 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL001.TXT")));
Set<String> set2 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL003.TXT")));
Set<String> onlyInSet1 = new HashSet<>(set1);
onlyInSet1.removeAll(set2);
Set<String> onlyInSet2 = new HashSet<>(set2);
onlyInSet2.removeAll(set1);