我想比较两个或多个文本文件以查找重复条目。O/P应该说明文件中的这些行是否匹配。
我想将文件1的每一行与文件夹2中的所有行进行比较(即,将文件1的第1行与文件2的所有行相比较(。当我运行下面的代码,将文件1的第1行与文件2的所有行进行比较时,程序终止。
注意:我尝试了Danail Alexiev的想法(见答案(,但循环正在无限运行,(也没有跳到文件1的2行,文件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:
=---匹配----=1321SC231231a23d1a32df1adfsdfsd-->1321sc231231a23D1a32df1=---不匹配----=1321sc231231a23d1a32df1dfsdfsdfsd-->fsdfs4dfs=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->dfsdf=---不匹配----=1321SC231231a23d1a32df1dfsdfsd-->3sd1f=---不匹配----=1321SC231231a23d1a32df1dfsdfsdfsd-->sdfs4df3s=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->df0=---不匹配----=1321SC231231a23d1a32df1dfsdfsd-->sd4f=---不匹配----=1321SC231231a23d1a32df1dfsdfsd-->sdf=---不匹配----=1321SC231231a23d1a32df1adsfsdfsd-->sdf1=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->3sdf=---不匹配----=1321SC231231a23d1a32df1dfsdfsdfsd-->sdfs4df6s=---不匹配----=1321SC231231a23d1a32df1dfsdfsdfsd-->fs1df=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->3sdfsd=---不匹配----=1321SC231231a23d1a32df1dfsdfsdfsd-->fs.d1f=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->s3d1=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->sdf1s=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->df1=---不匹配----=1321SC231231a23d1a32df1adfsdfsd-->sdf1sdf
将第二个文件的每一行与第一个文件的各行进行比较。
为了进行正确的比较,您必须检查匹配的行号。
循环时将您更改为:
while (((sCurrentLine1 = file1.readLine()) != null) && ((sCurrentLine2 = file2.readLine()) != null) {
// your comparison
}
请确保包含一个检查,以检测文件具有不同行数的情况。
编辑:
经过OP的澄清,我想我知道这个问题了。
你正在努力边读边读文件。当比较File1中的第一行和File2中的所有行时,嵌套while循环将停止,因为您已经读取了文件中的所有行都readLine()
每次都会返回null
。
要解决此问题,您需要提前读取File2中的所有行,并使用它们与File1中的行进行比较。
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;
}
输入:
文件1:
java
c++
test
stack
test
overflow
extraLine
文件2:
stack
overflow
test
java
c++
test
Ouput
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
找到了解决方案,谢谢大家