JAVA program for 2 or more text files comparing -
i want compare 2 or more text files find duplicate entry. o/p should lines in files matched or not.
i want compare each of file 1 lines lines of file 2 (ie., comparing file 1's line-1 lines of file 2). when run below code compares line 1 of file 1 lines of file 2, program got terminated.
note: tried danail alexiev's idea (see answer) loop running infinitely , (also not jumped 2 line of file 1, infinite loop on file 1's line 1 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
you comparing each line of second file each line of first file.
in order right comparison, have check matching line numbers.
change while loop to:
while (((scurrentline1 = file1.readline()) != null) && ((scurrentline2 = file2.readline()) != null) { // comparison }
be sure include check detect case files have different number of lines.
edit:
after clarification op, think know problem.
you trying read files go. when compare first line file1 lines file2, nested while loop stop, because have read lines file , readline()
return null
each time.
to fix this, need read lines file2 in advance, , use them compare against lines of file1.
Comments
Post a Comment