Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Skipping to a particular line while reading Text File

 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Readers

I am reading one Text File and , i want to skip lines to point to certain location , say 10th line . Is there any way of doing it in JAVA ?

Regards
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read 9 lines, don't do anything with them... Process only the 10th line when you read...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it depends. Do you want to skip a certain number of lines? do you want to skip lines until you get to one with a certain value in it? do you want to skip 9 lines, do something on the 10th, then skip 9 more?

You can do whatever you want, you just need to define what that is.
 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have META data in first line of file which says to go to the given number of lines and read data from there.

META DATA : 20 30 46 99

For example , the above line says that go to line number 20 and read some lines of data (depends on some token where to end that), then go to line number 30 and so on.....

What is the best way to do this.... ???

I am already doing the way , like reading lines and doing nothing with it, but that seems not to be an efficient way especially when my file is long.


Regards
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of ways of doing that. You can have for loops. If you have several lines like that, you can try putting them into a Set<Integer> using Integer.valueOf("20") to parse the tokens. Then you have a counter for the lines you have read, and you can see whether the Set contains that number.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course the hard part is working out whether line 20 is 20 lines beyond the meta-data line or 19 lines beyond it
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the lines a fixed size - i.e. each line is ALWAYS 27 characters long?

If not, I don't believe there is any other way. Forget java...how would YOU find the 30th line in a file without reading them one by one and counting?
 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Lines are not of fixed length...

Does BufferedReader.skip method an efficient over the following lines of codes

while loop // Till i skip necessary number of lines
line = reader.readline()


Actually , i have .vcf file in which there are various vcard's . But i want to pick particular vcard from that file . The only thing that is bothering me is that , suppose there are 2000 vcards in
a file and i want to pick the 1999th card, how to efficiently move to the 1999th card .

For those who dont know , each vcard is a name value pair which starts with BEGIN:VCARD and ends with END:VCARD. So i have a file as follows

...............FILE...................

BEGIN:VCARD
.
.
..
.
.
.
.
,
END:VCARD


BEGIN:VCARD
.
.
.
.
.
.
.
END:VCARD

.
.
.
.
.
.
.


BEGIN:VCARD
.
..
.
END:VCARD

.......................................................................................EOF...........................................................................


Regards
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Reddy wrote:The only thing that is bothering me is that , suppose there are 2000 vcards in
a file and i want to pick the 1999th card, how to efficiently move to the 1999th card .


Then you just read through the file, skipping over the cards you don't want until you get to the one you do want.

If that turns out to take too much time, and it becomes a problem, then the solution is to use a different way of storing that data which allows random access. But don't jump into redesigning that data file until it becomes apparent that there really is a perfomance problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the skip method will help you.
 
Sahil Reddy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Sahil Reddy wrote:The only thing that is bothering me is that , suppose there are 2000 vcards in
a file and i want to pick the 1999th card, how to efficiently move to the 1999th card .


Then you just read through the file, skipping over the cards you don't want until you get to the one you do want.

If that turns out to take too much time, and it becomes a problem, then the solution is to use a different way of storing that data which allows random access. But don't jump into redesigning that data file until it becomes apparent that there really is a perfomance problem.


Respected reader, I am already using this approach , but feels like there would be a performance issue.....

Let me go through random access file thing and get back to this thread....

Regards
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Reddy wrote:I am already using this approach , but feels like there would be a performance issue.....

What Paul is getting at is that "feels like" is bad guidance for performance optimizations. Only if you have hard numbers that show conclusively that performance is a problem should you start optimizing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic