Re: FileReader / BufferedReader Help
DJ wrote:
Knute,
Okay - I'll take my lumps now. I missed the whole "not including any
line-termination characters..." bit. Guess I was glossing over the material
too quickly..
Moving forward, since the line terminators are important, I can see I'll
have to use read instead of readLine as Roedy suggested. That said, I'm not
clear on how to implement read to get an entire lines' worth of text from
each file so that I can compare the files "line" for "line". Read returns an
integer value for each character, but how do I determine when I've read the
entire line? Test for a crlf (10/13) pair? I'm also not sure how to convert
read's int values to characters so I can write the lines that don't match to
my results file.
Any clues? I'm not looking to have the code written for me. Rather, just
point me in the right direction(s).
BTW, I know where the docs live. And I really am trying to get more out of
this exercise than honing my copy&paste skills. ;)
Thanks,
~ Dave
"Knute Johnson" <nospam@rabbitbrush.frazmtn.com> wrote in message
news:pCQti.38753$dA7.32233@newsfe16.lga...
DJ wrote:
Perhaps my use of "terminator" is incorrect.
I have two simple text files such that file1.txt consists of a single
line of text (no crlf), and file2.txt consists of the identical line of
text followed by a second blank line (implying the line is terminated
with crlf, no?). After retrieving each line using readLine and assigning
the lines to strings, the equals method indicates the two strings are
equal.
I would have thought they'd be different given that the line read from
file2.txt is terminated with crlf.
"Roedy Green" <see_website@mindprod.com.invalid> wrote in message
news:bgafb3pcppk35t1oa1j4l3a1t3mfsmndpb@4ax.com...
On Mon, 6 Aug 2007 18:40:42 -0400, "DJ" <ddjames@tampabay.rr.com>
wrote, quoted or indirectly quoted someone who said :
For instance, a line of text terminated with crlf (newline) was
determined equal to the same line of text without the crlf termination.
that makes no sense. You don't have a line unless there is a
terminator. BufferedReader treats \r\n and \n both as line
terminators in Windows.
If you were interested in the details of the line terminator, use read
rather than readLine so that it gets passed through to the app.
See http://mindprod.com/applet/fileio.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Do you know where to find the docs?
http://java.sun.com/javase/6/docs/
See BufferedReader
"readLine
public String readLine()
throws IOException
Reads a line of text. A line is considered to be terminated by any one
of a line feed ('\n'), a carriage return ('\r'), or a carriage return
followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any
line-termination characters, or null if the end of the stream has been
reached
Throws:
IOException - If an I/O error occurs"
--
Knute Johnson
email s/nospam/knute/
The question is do you really care if the lines are terminated
differently. Text files all have one of the three termination schemes,
that's what makes them text files. If your files differ in termination
then every line will be different. If your files use different
termination schemes for different lines of text then you've got a real mess.
You haven't explained the reason for the file compare. Maybe if you did
that we would have a better idea of how to proceed.
If you really need to record the termination, I suggest that you write a
method similar to readLine() that keeps the terminators. Then you can
convert them to strings with the appropriate encoding and compare them.
If you need to display them as text and identify the different
terminators you could use a substitution method that would produce
something like this;
this is a line of text<CR>
this is a line of text<LF>
--
Knute Johnson
email s/nospam/knute/