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.
Never ascribe to malice that which can be adequately explained by stupidity.
Sahil Reddy
Ranch Hand
Joined: Jan 24, 2011
Posts: 143
posted
0
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
Joined: Oct 13, 2005
Posts: 32611
4
posted
0
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
Joined: Oct 13, 2005
Posts: 32611
4
posted
0
Of course the hard part is working out whether line 20 is 20 lines beyond the meta-data line or 19 lines beyond it
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
Joined: Jan 24, 2011
Posts: 143
posted
0
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
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
Joined: Oct 13, 2005
Posts: 32611
4
posted
0
I don't think the skip method will help you.
Sahil Reddy
Ranch Hand
Joined: Jan 24, 2011
Posts: 143
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
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.