This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes I need help with seeking to abitiary positions within a text file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "I need help with seeking to abitiary positions within a text file." Watch "I need help with seeking to abitiary positions within a text file." New topic
Author

I need help with seeking to abitiary positions within a text file.

Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Greetings everyone,
I have this school assignment which states,
Design and write a program to compare the contents of any two text files. The files should be compared line by line with a simple message beingb displayed, such as: The following lines are different, followed by the contents of the two lines, whienever a line from the first file is not the same as ta line from the second file. When the endo of the one (or both) of the files is reached the comparision is terminated and one of the following messages is displayed:
Files are of unequal length., Files are of the same length but different contents., or Files are the same.

For which I wrote the following code:

The program now works almost perfect but I am having some difficulty seeking to positions within the files. The lines I labelled X & Y don't actually do what I intend them to do. All I keep getting when I click the OK button is a comparison of the first lines of the two files. And I would like the comparison to start on the next line from the one that had just been compared.
I also can't seem to figure out how to seek to the end of the files and terminate the comparison as as the question asks me to do.
Any useful suggestions would be appreciated.


SCJP
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
firstFileContents (and secondFileContents) are only being assigned a new value once. You probably want them to change from time to time.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Also:

The getFilePointer() method reports where in the file the next read will occur (i.e. what's the current position); seek() tells the RAF to go to that position. You're basically telling the RAF to go to the position it's already at. This has no effect at all.
I'm not sure there's really any use for a RandomAccessFile here. If your goal is to simply read the very next line, immediately after the last line read - readLine() (found in BufferedReader or LineNumberReader) will do that.
Try this: forget about reading two files and comparing them for a moment. Try to write a program which can read from just one file, and print out each line after it's read. Once you learn how to do this, then modify the program to read from two files silmultanteously, printing each one. Than, modify further to actually compare the two lines, and print based on the results. Don't try to do too many new things at once when you're learning - it just makes things harder. Good luck...


"I'm not back." - Bill Harding, Twister
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Greetings!,
Thanks for the explanation David Weitzman and Jim Yingst. They looked right,like something out of a Java textbook, but it didn't still work for me. The only reason why I used the random access file was to be able to seek to the end of the file (or to be able to use the seek() method and the getFilePointer()) since BuffredReader and LineNumberReader's readLine() wasn't giving me the desired result no matter how hard I tried. I have used this method before and got what I wanted but there's something wrong with this code (which I am unable to figure out). I also suspected that I was firstFileContents and secondFileContents were being assigned a new value only once. I got that corrected, but the code still only reads the first line of both files. Can you please take a close look again.
Thanks.
Just so you see, the code now looks like this:
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
A few points
You're discarding the first line of each file without actually processing it.
The LineNumberReader will always claim that you're still on the first line. It can only count lines if you actually read input through it. Try wrapping it around the BufferedReader (instead of the FileReader) and replacing calls to bfr#.readLine() with lnr#.readLine();
Every call to setText() will delete any text that used to be in the text area. What you want to do is append text, not replace it.
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Thanks David Weitzman. It's now working, after doing just what you said. Thanks. I now know better..... One last question though, you said;
You're discarding the first line of each file without actually processing it.
. That's obviously true as there are no results from the first lines displayed in the textArea no matter how the lines in the files are. I've gone through the code again and again and I can't see why the first line of each file gets discarded without processing. Could you just kindly point this out to me? Thanks.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Notice that the code
firstFileContents = bfr1.readLine(); secondFileContents = bfr2.readLine();
appears twice.
The first time is immediately after the BufferedReaders are created (it reads in the first line), and the second is in the loop (it reads from the second line on). The loop doesn't do anything with the values of the first lines that get read before the loop, so the values are simply lost.
By eliminating the readLine()s before the loop, you let the loop access all the lines, and the problem is solved.
Femi Alla
Ranch Hand

Joined: Jul 05, 2002
Posts: 79
Thanks.
It's now perfect.
You're good.
 
jQuery in Action, 2nd edition
 
subject: I need help with seeking to abitiary positions within a text file.
 
Similar Threads
Log File Reader in JAVA
Why does File renameTo() fails ?
LinearProgramming in Java
Help,Components inside my Frame would not be displayed until maximized.
declaring array of vectors