• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to read a line based on line number

 
JP Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15451
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" ...
 
JP Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15451
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic