The moose likes Java in General and the fly likes Skip Reading several Lines using BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Skip Reading several Lines using BufferedReader" Watch "Skip Reading several Lines using BufferedReader" New topic
Author

Skip Reading several Lines using BufferedReader

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 91

Hi guys I am reading a text file but I do not want to read the whole of it. Is there a way I can make the Buffered Reader ignore or skip reading the lines between 57 and 88 and also the lines between 129 and 160?

Here is the code I am using



Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
No, I don’t think you can. You can count the lines and simply ignore those with the inappropriate numbers.But hard-coding such numbers and ranges is very error-prone, leading to brittle code if there are any changes in the format of your file.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 91

Thank you Ritchie for that insight. The lines I want to Skip have similar content. I am wondering whether we can have a code for :
if line starts with "STATEMENT OF ACCOUNT"
then
Skip 12 lines above it.
Is there anything like that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26720
You could do that easily if you had the lines in a List<String>. You can simply remove the last 12 lines before adding the next line. But be very careful that you really need to remove 12 lines, not 11 or 13 sometimes.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 91

Ritchie, am very new to Java so I would not know how to do that. A sample code please?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3144
Stanley Mungai wrote:Ritchie, am very new to Java so I would not know how to do that. A sample code please?


Look at the documentation for java.util.List, and if you can't figure anything out, google for java collecitons tutorial.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8428

Stanley Mungai wrote:Ritchie, am very new to Java so I would not know how to do that. A sample code please?

We don't provide code for you here. We feel it works best when you write the code and then come back with questions about why it does or doesn't work how you expect. So, give it a shot yourself, then come back and let us know where you get stuck.


Never ascribe to malice that which can be adequately explained by stupidity.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 3905
you might want to try Campbells original suggestion first(even though i agree the style sucks) that can get it "working" then try making it better. i have compared to "starts with" before


I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 1610

Stanley Mungai wrote:Hi guys I am reading a text file but I do not want to read the whole of it. Is there a way I can make the Buffered Reader ignore or skip reading the lines between 57 and 88 and also the lines between 129 and 160?

Java is an Object-Oriented language, so here's a thought: create a Class that does it for you.

I suspect mine might look something like this (I've only included the basics):and there's probably several ways to improve performance as well.
I leave the Range class up to you (me, I'd probably just extend java.awt.Point and add a contains() method; but I'm lazy )

HIH

Winston

[Edit] Actually, Point is a bad choice. It's inherently unsafe. But a wrapper to a Point...


More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
 
 
subject: Skip Reading several Lines using BufferedReader
 
Threads others viewed
bufferedReader.readLine() random order of lines ?
Problem reading a file
Reading Java File
Problem with Reading very long line using bufferedReader
Reading one line from a text
developer file tools