my dog learned polymorphism*
The moose likes Java in General and the fly likes Skip Reading several Lines using BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 155

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: 39409
    
  28
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: 155

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: 39409
    
  28
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: 155

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: 6109
    
    6

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: 11411
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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...


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Skip Reading several Lines using BufferedReader