我想比较两个或更多文本文件以查找重复条目。 O / P应该说文件中的那些行是否匹配。
我想将每个文件1 行与文件2 的所有行进行比较(即,比较文件1'第1行文件的所有行2)。 当我运行下面的代码时,它将文件1的第1行与文件2的所有行进行比较,然后程序被终止。
注意:我尝试了 Danail Alexiev 的想法(查看答案),但循环无限运行,(也没有跳到2行文件1,文件1'第1行的无限循环,文件2的所有行
以下文件
文件1:内容
21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s
df0
sd4f
sdf
sdf1
3sdf
sdfs4df6s
fs1df
3sdfsd
fs.d1f
s3d1
sdf1s
df1
sdf1sdf
文件2:内容
21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s
df0
sd4f
sdf
sdf1
3sdf
sdfs4df6s
fs1df
3sdfsd
fs.d1f
s3d1
sdf1s
df1
sdf1sdf
代码:
while ((sCurrentLine1 =file1.readLine()) != null )
{
while ((sCurrentLine2 =file2.readLine()) != null )
{
if(sCurrentLine1.equalsIgnoreCase(sCurrentLine2))
{
System.out.println("=---Matched----=" + sCurrentLine1 + " -->" + sCurrentLine2);
}
else
{
System.out.println("=---Not Matched----=" + sCurrentLine1 + " -->" + sCurrentLine2);
}
}
}
O / P :
= --- Matched ---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 1321sc231231a23d1a32df1adfsdfsdfsd = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > fsdfs4dfs = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > dfsdf = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sd1f = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdfs4df3s = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > df0 = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sd4f = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdf = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdf1 = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sdf = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdfs4df6s = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > fs1df = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sdfsd = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > fs.d1f = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > s3d1 = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdf1s = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > df1 = ---未匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdf1sdf
答案 0 :(得分:1)
您正在将第二个文件的每一行与第一个文件的每一行进行比较。
为了进行正确的比较,您必须检查匹配的行号。
将循环更改为:
while (((sCurrentLine1 = file1.readLine()) != null) && ((sCurrentLine2 = file2.readLine()) != null) {
// your comparison
}
请务必检查以检测文件行数不同的情况。
修改强>
在OP澄清之后,我想我知道这个问题。
您正在尝试阅读文件。当您将File1的第一行与File2中的所有行进行比较时,嵌套while循环将停止,因为您已经读取了文件中的所有行,readLine()
每次都会返回null
。
要解决此问题,您需要提前读取File2中的所有行,并使用它们与File1的行进行比较。
答案 1 :(得分:0)
public void CompareFileMain()
{
/*int tempvar = 0;
int tempvar1 =0 ;*/
String Tlines1[] = new String [100];
String Tlines2[] = new String [100];
String tempstring1 = null;
String tempstring2 = null;
int file1lines;
int file2lines ;
System.out.println(file1lines = linesOffile1());
System.out.println(file2lines = linesOffile2());
try {
file1 = new BufferedReader(new FileReader(SOAPFile));
file2 = new BufferedReader(new FileReader(RestFile));
int oplines = 1;
while ((sCurrentLine1 =file1.readLine()) != null )
{
tempstring1 = sCurrentLine1;
while ((sCurrentLine2 =file2.readLine()) != null )
{
tempstring2 = sCurrentLine2;
}
if ((sCurrentLine2 =file2.readLine()) == null)
{
System.out.println("File 1 --> line# "+oplines +"\t");
CompareFileSub(tempstring1, tempstring2);
}
oplines = oplines+1;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void CompareFileSub(String t1, String t2) throws IOException // reading file 2
{
file2 = new BufferedReader(new FileReader(RestFile));
int oplines = 1;
while ((sCurrentLine2 =file2.readLine()) != null )
{
t2 = sCurrentLine2;
if((t1.contains(t2) && (t2.contains(t1))))
{
System.out.print(t1+"-->"+t2+"---->matched \t");
System.out.println("File 2 --> line# "+oplines +"\n");
}
else
{
System.out.print(t1+"-->"+t2+"---->not matched \t");
System.out.println("File 2 --> line# "+oplines +"\n");
}
oplines = oplines+1;
}
}
public int linesOffile1() //Count file 1 lines
{
try {
while ((sCurrentLine1 =file1.readLine()) != null )
{
//System.out.println("Taken line sCurrentLine1 "+sCurrentLine1);
//System.out.println("Taken line sCurrentLine1 "+i);
i=i+1;
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (file1 == null)file1.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return i;
}
public int linesOffile2() //Count file 2 lines
{
try {
while ((sCurrentLine2 =file2.readLine()) != null )
{
//System.out.println("Taken line sCurrentLine2 "+sCurrentLine2);
//System.out.println("Taken line sCurrentLine2 "+j);
j=j+1;
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (file2 == null)file1.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return j;
}
输入:
File1中:
java
c++
test
stack
test
overflow
extraLine
文件2:
stack
overflow
test
java
c++
test
输出继电器
8
7
File 1 --> line# 1
java-->stack---->not matched File 2 --> line# 1
java-->overflow---->not matched File 2 --> line# 2
java-->test---->not matched File 2 --> line# 3
java-->java---->matched File 2 --> line# 4
java-->c++---->not matched File 2 --> line# 5
java-->---->not matched File 2 --> line# 6
java-->test---->not matched File 2 --> line# 7
File 1 --> line# 2
c++-->stack---->not matched File 2 --> line# 1
c++-->overflow---->not matched File 2 --> line# 2
c++-->test---->not matched File 2 --> line# 3
c++-->java---->not matched File 2 --> line# 4
c++-->c++---->matched File 2 --> line# 5
c++-->---->not matched File 2 --> line# 6
c++-->test---->not matched File 2 --> line# 7
File 1 --> line# 3
-->stack---->not matched File 2 --> line# 1
-->overflow---->not matched File 2 --> line# 2
-->test---->not matched File 2 --> line# 3
-->java---->not matched File 2 --> line# 4
-->c++---->not matched File 2 --> line# 5
-->---->matched File 2 --> line# 6
-->test---->not matched File 2 --> line# 7
File 1 --> line# 4
test-->stack---->not matched File 2 --> line# 1
test-->overflow---->not matched File 2 --> line# 2
test-->test---->matched File 2 --> line# 3
test-->java---->not matched File 2 --> line# 4
test-->c++---->not matched File 2 --> line# 5
test-->---->not matched File 2 --> line# 6
test-->test---->matched File 2 --> line# 7
File 1 --> line# 5
stack-->stack---->matched File 2 --> line# 1
stack-->overflow---->not matched File 2 --> line# 2
stack-->test---->not matched File 2 --> line# 3
stack-->java---->not matched File 2 --> line# 4
stack-->c++---->not matched File 2 --> line# 5
stack-->---->not matched File 2 --> line# 6
stack-->test---->not matched File 2 --> line# 7
File 1 --> line# 6
test-->stack---->not matched File 2 --> line# 1
test-->overflow---->not matched File 2 --> line# 2
test-->test---->matched File 2 --> line# 3
test-->java---->not matched File 2 --> line# 4
test-->c++---->not matched File 2 --> line# 5
test-->---->not matched File 2 --> line# 6
test-->test---->matched File 2 --> line# 7
File 1 --> line# 7
overflow-->stack---->not matched File 2 --> line# 1
overflow-->overflow---->matched File 2 --> line# 2
overflow-->test---->not matched File 2 --> line# 3
overflow-->java---->not matched File 2 --> line# 4
overflow-->c++---->not matched File 2 --> line# 5
overflow-->---->not matched File 2 --> line# 6
overflow-->test---->not matched File 2 --> line# 7
File 1 --> line# 8
extraLine-->stack---->not matched File 2 --> line# 1
extraLine-->overflow---->not matched File 2 --> line# 2
extraLine-->test---->not matched File 2 --> line# 3
extraLine-->java---->not matched File 2 --> line# 4
extraLine-->c++---->not matched File 2 --> line# 5
extraLine-->---->not matched File 2 --> line# 6
extraLine-->test---->not matched File 2 --> line# 7
得到解决方案,谢谢大家