Do you know how to write a while loop? That is the easiest way. Bertrand Meyer curses this bit of syntax as awkward and hard to read:See, when you get beyond the last line, you get a null line; then the loop will terminate. You are doing two things inside the (). The first thing you are doing is reading the line, then you are checking it isn't null. So reads the line, with the result that your variable line now is equal to the line. The bit aboutchecks that you haven't got a null line.
Why do you need the extra (), I hear you ask? Because the != operator has a higher precedence than the = operator. The extra pair of () makes the JVM read the line before you test whether it is null.
So now you know how that bit of awkward syntax works, you can use it. It doesn't count 4 lines or anything, but keeps going until it has got the last line.
My guess would be that Sandeep is opening a new BufferedReader to read each line; I've seen folks make that mistake before. The trick is to create just one BufferedReader, then call readLine() on that one object as many times as you need. So the "new BufferedReader()" line, you do that just once.
Originally posted by Kaydell Leavitt: Does it matter what the end-of-line character(s) are?
I don't think so, no. Probably best to keep them the same as everything else in line with your OS, though:
Windows/DOS = \r\n
Mac = \r
Unix and Linux, and I think Solaris too = \n
Joined: Sep 29, 2002
If files need to be transferred between OSs, I've sometimes used the dos2unix and unix2dos utilities to translate the CR/LF characters.
Some files have no newlines characters. This will date me horrendously - I remember working on mainframe systems where the files were of fixed, 80-character length, thus mimicking the old punched cards. In the unlikely event of having to process such a file, you would have to append the LF character to each record.