This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 8836

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

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[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
It is sorta covered in the JavaRanch Style Guide.
subject: read blocks of lines from file
Similar Threads
Skipping to a particular line while reading Text File
Find last line number in a file
Accessing a File
Reading no of lines from a file
What is the Best way to determine # of line in a...