readLine(int x)?

G

Guest

Is there a way to read a specific line from a file, so for example, I'm
wanting to achieve the folllowing:

String s = thing.readLine(54);

? Is there a DataInputStream or BufferedThing, etc that can do this?

TIA
 
F

Furious George

Is there a way to read a specific line from a file, so for example, I'm
wanting to achieve the folllowing:

String s = thing.readLine(54);

? Is there a DataInputStream or BufferedThing, etc that can do this?

You could use a BufferedReader to read (and silently discard) 53 lines
and then read the 54th line.
String line = null ;
for ( int i = 0 ; i < 54 ; i ++ )
{
line = bufferedReader . readLine ( ) ;
}

If you know the position that the 54th line starts, you could use a
RandomAccessFile
long positionWhereLine54Starts ;
raf.seek(positionWhereLine54Starts);
String line=raf.readLine();
 
G

Guest

You could use a BufferedReader to read (and silently discard) 53 lines
and then read the 54th line.
String line = null ;
for ( int i = 0 ; i < 54 ; i ++ )
{
line = bufferedReader . readLine ( ) ;
}

If you know the position that the 54th line starts, you could use a
RandomAccessFile
long positionWhereLine54Starts ;
raf.seek(positionWhereLine54Starts);
String line=raf.readLine();

Hmm, thanks for the help - much appreciated, but unfortunately this isn't
very practical for my purposes - I'm using a Random object to generate the
line number and then I want to copy the String contents of that line out.
The lines are all different and there could be thousands of lines, so your
initial suggestion isn't the most efficient, but may be workable and the
second suggestion isn't an option unfortunately, but like I said - many
thanks for the suggestions!
 
C

Chris Uppal

The lines are all different and there could be thousands of lines, so your
initial suggestion isn't the most efficient, but may be workable

If you are only doing this once (and don't know the lengths of the lines) then
George's suggestion is not only workable, it's the only technique available ;-)

If you intend to extract arbitrary lines from the same file more than a few
times, then it might be worth building (either in memory, or in a separate
file) a table of the positions and lengths of each line, and then using that,
plus RandomAccessFile, to extract arbitrary lines on-demand. You build the
table once, and use it many times, so the initial effort will pay for itself if
you use it often enough.

-- chris
 
M

Mark Jeffcoat

Hmm, thanks for the help - much appreciated, but unfortunately this isn't
very practical for my purposes - I'm using a Random object to generate the
line number and then I want to copy the String contents of that line out.
The lines are all different and there could be thousands of lines, so your
initial suggestion isn't the most efficient, but may be workable and the
second suggestion isn't an option unfortunately, but like I said - many
thanks for the suggestions!


If the only way to count lines is by counting EOL characters,
then something's going to have to do it; there's not going to
be a big efficiency difference between doing it in your code
and having it done by a library.


On the other hand, if the file isn't changing, you only have
to count them once. Then you can build index that maps line
numbers to byte offsets, and re-visit the RandomAccessFile idea.
 
H

Hal Rosser

Is there a way to read a specific line from a file, so for example, I'm
wanting to achieve the folllowing:

String s = thing.readLine(54);

? Is there a DataInputStream or BufferedThing, etc that can do this?

TIA

The easiest way is to read it (the file) one time - into a string.
Then split that string into an array - using eol char as delimiter.
Then when you want line 55 - use 55 as the index of the array.
Get another number - use it as an index, etc etc
tada
 
F

Furious George

Hmm, thanks for the help - much appreciated, but unfortunately this isn't
very practical for my purposes - I'm using a Random object to generate the
line number and then I want to copy the String contents of that line out.
The lines are all different and there could be thousands of lines, so your

thousands of lines is not that many to a computer.
initial suggestion isn't the most efficient, but may be workable and the
second suggestion isn't an option unfortunately, but like I said - many
thanks for the suggestions!

It all depends on what you want. If you just want some random line
then you could:

long randomSeek ; // randomly from ( 0 , file.length )
raf.seek(randomSeek);
String bogusLine=raf.readLine(); // ignore this line because it is
probably incomplete
String realLine = raf.readLine(); // this is the first whole line after
your seek position

Of course,
(1) you would have to insert some logic to protect yourself in case you
randomly chose the the last line and tried to read past the end of the
file.
(2) this would not be a uniform distribution. Lines after long lines
would be chosen with greater frequency than lines after short lines.
But this may be good enough for your needs.
 
F

Furious George

Tor said:
AFAIK readLine() would then just return null.

The OP would have to figure that one out.
Of course then you would
need to call again until you actually got a line. And you would get an
infinite loop if the file happened to have just one line unless you
added a restriction.

If the file happens to have just one line, then the OP's problem is
trivial.
 
T

Tor Iver Wilhelmsen

Furious George said:
(1) you would have to insert some logic to protect yourself in case you
randomly chose the the last line and tried to read past the end of the
file.

AFAIK readLine() would then just return null. Of course then you would
need to call again until you actually got a line. And you would get an
infinite loop if the file happened to have just one line unless you
added a restriction.
 
S

Simon Brooke

Hal Rosser said:
The easiest way is to read it (the file) one time - into a string.

Whoo!

No, don't do that. You don't know how big this file is. The read one line
at a time approach is much safer.

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/

The Conservative Party is now dead. The corpse may still be
twitching, but resurrection is not an option - unless Satan
chucks them out of Hell as too objectionable even for him.
 
G

Guest

thousands of lines is not that many to a computer.

Actually, in the end, I figured the same - the text file may be up to
several thousand lines (only under a meg in size) and the method I wrote in
conjunction with your first suggestion works very well at what it needs to
do, so I'm happy.

Thanks for all the suggestions asnd help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top