File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Counting blank lines in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting blank lines in a file" Watch "Counting blank lines in a file" New topic

Counting blank lines in a file

Michael Allan

Joined: Mar 04, 2003
Posts: 4
I have a very simple code snippet which is counting the number of lines in a text file by sequentially reading the lines a BufferedReader and incrementing a counter. There's no problem reading lines with text, or even single blank lines between paragraphs, but once the readLine() method encounters multiple blank lines together it *sometimes* fails to count one or more lines, according to a pattern I can't quite determine.
Does anyone know what sort of checks I need to perform to make sure that all lines (even the blank ones) are counted until the EOF?
Michael Allan

Joined: Mar 04, 2003
Posts: 4
OK, it just didn't make sense, so I checked out the test file in another program ... and lo and behold, no problem. D'oh!
Pity about having already made a public fool of myself. Ah well, probably won't be the last time.
Real questions to follow soon, hopefully.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It's possible that the problem is that differnt files have different types of line separators, and the number of lines depends on what you consider a line separator. On Unix \n is a separator, while on Windows it's \r\n, and on Mac I think it's \n\r maybe. BufferedReader and many programs have a flexible interpretation which may include each/all of these as lines. So consider something like:
If you look at this in Notepad, it's two lines (with a funny box just before "bar", indicating the lone \n. In Unix, it's three lines (middle line blank, with a funny char after "foo".) On a Mac, it may look like one line. Using BufferedReader, it's three lines, middle line blank, no funny chars.
OK, so this is complex, I know. What it boils down to is, you need to consider whether this is worth worrying about, and if so, what exactly do you want to consider the definition of a line? Note that the BufferedReader API for readLine() explains exactly how that method works. If you want to use a different definition, you'll have to implement it yourself.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Counting blank lines in a file
jQuery in Action, 3rd edition