permaculture playing cards
The moose likes I/O and Streams and the fly likes read blocks of lines from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "read blocks of lines from file" Watch "read blocks of lines from file" New topic

read blocks of lines from file

Sashi Gundoji

Joined: Jan 30, 2007
Posts: 10

I am new to java. I have a large file it contains the lines. Each line will be terminated with \n.

I do not want to read all lines at once, I would like to read some blocks of lines at once.

I really did not find a method to read specified number of lines. I found there some methods which are reading the number of bytes.

If any body know how to read please send me the source.

Thanking you

Joe Ess

Joined: Oct 29, 2001
Posts: 9169

Have a look at Stan's answer to this question from yesterday.

[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's a little clearer this time around ... you can make a String array as a "buffer" of some number of lines. Read lines into the array and process the array. Repeat until you hit end of file.

I'm curious why a buffer of 10 or 100 or 1000 lines might be better than, say, 1. Read a line, process it. Java's BufferedReader already optimizes physical disk reads with buffering very much like the string array just so we don't have to worry about such things.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: read blocks of lines from file
jQuery in Action, 3rd edition