This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes to read a line based on line number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "to read a line based on line number" Watch "to read a line based on line number" New topic
Author

to read a line based on line number

JP Singh
Greenhorn

Joined: Oct 10, 2006
Posts: 3
Hi Ranchers,

Can anybody help me in writing a code for:
>> reading a line based on the particular line number.
Complete requirement is to search for a particular String in the file and then to display line in which it is found. Along with that line, need to show the specified (2 0r 3) line before and after the line which contains the searched String.

Please help !!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Use the method readLine() in class java.io.BufferedReader to read your file line by line. Count the lines, and find the line you are looking for that way.

Lookup class BufferedReader in the Java API documentation.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
First read following examples
Reading Text from a File
Searching a String for a Character or a Substring

Apply some logic, and you will be able to write your desired code.


My blood is tested +ve for Java.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Even better, the java.io.BufferedReader has a subclass named java.io.LineNumberReader. This latter has method getLineNumber() that can be very useful to your intentions.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The usual loop:

won't help you show the few lines before and after the match. What are your thoughts on that? Let's challenge ourselves and say the file is too big to keep it all in memory in an array of lines or something. One idea - Google for "circular buffer" ...


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
JP Singh
Greenhorn

Joined: Oct 10, 2006
Posts: 3
Requirement is to fetch the previous lines once you have found the searched string.
For example: if i found the searched string on line number 7, i need to go back by 2 lines and want to fetch lines 5th, 6th and 7th.

option of using LineNumberReader is not helpful, as setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
For the requirement you've given, I would simply use two local variables to remember the previous two lines. Every time you read a new line, shift the contents of the variables, so that you always remember the most recent three lines. Or a LinkedList or other Queue implementation would work nicely here, especially if you may need more than two previous lines in the future. Every time you read a line, add it to the end of the list - and if the size is greater than 3 (or whatever), remove one line from the beginning.


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

option of using LineNumberReader is not helpful, as setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

It is helpful, but you still have to read the lines until you arrive at the line you are looking for. LineNumberReader just makes it easy to track at which line you are.

For showing the previous few lines: just remember the last few lines you've read, and display them if you arrive at the line you're looking for.

So, now you've been given a whole list of ideas, and you've thought about it yourself too. When are you going to write some code yourself? Write some code, try it out, if it doesn't work and you don't know how to continue post it here, and we'll help you to get on with it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: to read a line based on line number