I want to compare a 2 or more Text Files to find a duplicate entry. O/P should say those lines in files are matched or not.
I want to compare the each of the File 1 lines with all lines of the File 2 (ie., Comparing File 1's line-1 with all lines of File 2). When I run the below code the that compares the line 1 of File 1 with all lines of File 2, then program got terminated.
Note: I tried Danail Alexiev's idea (See the answer) but the loop is running infinitely , (also not jumped to 2 line of File 1, infinite loop on File 1's line 1 with all lines of File 2)
Files Below
File 1: Content
21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s
df0
sd4f
sdf
sdf1
3sdf
sdfs4df6s
fs1df
3sdfsd
fs.d1f
s3d1
sdf1s
df1
sdf1sdf
File 2: Content
21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s
df0
sd4f
sdf
sdf1
3sdf
sdfs4df6s
fs1df
3sdfsd
fs.d1f
s3d1
sdf1s
df1
sdf1sdf
Code:
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 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fsdfs4dfs =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->dfsdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sd1f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdfs4df3s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->df0 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sd4f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdfs4df6s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fs1df =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sdfsd =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fs.d1f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->s3d1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->df1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1sdf
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;
}
InPut:
File1:
java
c++
test
stack
test
overflow
extraLine
File2:
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
Got the solution, thank you all