wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Counting blank lines in a file" Watch "Counting blank lines in a file" New topic
Author

Counting blank lines in a file

Michael Allan
Greenhorn

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
Greenhorn

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
Wanderer
Sheriff

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:
"foo\r\n\nbar"
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: http://aspose.com/file-tools
 
subject: Counting blank lines in a file
 
Similar Threads
Skip Reading several Lines using BufferedReader
nio performance issue
Read and write last lines
Help !!
help with counting operands and operaters