Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Skip Reading several Lines using BufferedReader

 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie, am very new to Java so I would not know how to do that. A sample code please?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic