aspose file tools*
The moose likes I/O and Streams and the fly likes comparing two files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "comparing two files" Watch "comparing two files" New topic
Author

comparing two files

divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
Hi All,
My intension is to grab the common lines from 2 files in which there are lines of 6 digits with a hyphen in the middle.
like say
123-652
982-648 and so on...........
It's surprising to me that the following code prints nothing though I have many lines in common in those 2 files. Can somebody please help where I am going wrong?



Thanks,
Divya
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9


I don't think this code does what you think it does. It sure doesn't compare every line in two files. Try printing out both line and str.
[ January 30, 2004: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
Strangly, nothing prints even when I print both the lines.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

That kind of depends on your data file. You may want to add some text to your System.out.println(). Like:

Just in case you have some blank lines or something weird in the data.
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Or maybe it's not even getting into the code where you've placed the printlns, because the file doesn't exist. I notice you are using relative path names, so the execution of your app depends on being launched in a particular directory, which is almost always a no-no. You should be reading files from the classpath instead of using directory names. For one reason, suppose you decide to jar-up the code and file(s) - then they are no longer files on a physical directory.
Use:
InputStream stream = YourClass.class.getResource("/yourfile.txt")
to open from the classpath (remember to close it afterwards as well).
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Originally posted by divya madala:

it prints only the first line of 1st file proir to every line of 2nd file

Any idea as to why this may be?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Originally posted by divya madala:
it prints only the first line of 1st file proir to every line of 2nd file

HINT:
Try printing out the values like this:

and make sure you label them in your System.out.println's.
kiranmayi Talusani
Greenhorn

Joined: Feb 01, 2004
Posts: 2
divya,
First your program is comparing first line from first file with all the lines from second file.

After that it is comparing all the lines from the first file with the last line(after first line is compared with all the lines from second file, the value in "str" will be last line from second file) from second file.I guess that is the reason you are not seeing the currect output.
kiranmayi Talusani
Greenhorn

Joined: Feb 01, 2004
Posts: 2
Divya,
I think your program is only comparing first line from the first file with all the lines from the second file,because the value of 'str' becomes 'null'.
when the program's execution comes out of the second(inner) while loop the value of 'str' will become 'null'. Then it will read second line from frist file.But it will not enter into the second(inner) while loop because the value in str is 'null'. so it will read all the lines from first file util the value in 'line' becomes 'null' and then it comes out of the first(outer) while loop.
himanshu patel
Ranch Hand

Joined: Feb 03, 2003
Posts: 205
Once your loop through values contains in String str, you have to start reading same file from first.
I do not remember whether FileReader does have any method to move file pointer to a specifiled poistion.So in your case, after first traverse, you str value will always be null and hence it will never compare.
Instead you can use RandomAccessFile class which have seek() methos which
set the FILE pointer wherever you want.In addition,you does not need to use
BuffereReader with RandomAccessFile class.
You can see this example.
if (fileName.exists()) {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
RandomAccessFile raf = new RandomAccessFile(fileName1, "r");
String line="";
String str="";
line = br.readLine();
while(line != null)
{
raf.seek(0);
str = raf.readLine();
while(str != null)
{
if(line.equalsIgnoreCase(str))
{
System.out.println("MATCHED :" + line);
}
str = raf.readLine();
}
line = br.readLine();
}
}

Hope this help
[ February 02, 2004: Message edited by: himanshu patel ]

If you want to become a rich, do not work for others but make others to work for you.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

kiranmayi and himanshu, easy on just giving the answers away. This is obviously a school assignment so we want our friend here to learn the valuable lesson of how to debug his own code!
divya madala
Ranch Hand

Joined: Jan 10, 2001
Posts: 61
Thanks so much for your answers Himanshu and Kiranmai. It helped a lot!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing two files