File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Skipping to a particular line while reading Text File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Skipping to a particular line while reading Text File" Watch "Skipping to a particular line while reading Text File" New topic
Author

Skipping to a particular line while reading Text File

Sahil Reddy
Ranch Hand

Joined: Jan 24, 2011
Posts: 145
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
Bartender

Joined: May 31, 2011
Posts: 1776
Read 9 lines, don't do anything with them... Process only the 10th line when you read...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

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.


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

Joined: Jan 24, 2011
Posts: 145
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: 40027
    
  28
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: 40027
    
  28
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

Joined: Oct 02, 2003
Posts: 11494
    
  16

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: 145
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
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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: 40027
    
  28
I don't think the skip method will help you.
Sahil Reddy
Ranch Hand

Joined: Jan 24, 2011
Posts: 145
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: 42906
    
  69
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Skipping to a particular line while reading Text File