Read RandomAccesFile (newbie)

C

cadsjo

Hi,

I want to read pieces from a RandomAccessFile
1 piece = a name (25 chars)
1 piece = a nr (7 chars)

Help me!
I don't know how to start and go further...

Thanks! John

My file looks like this
name a 1234567name b 7654321name c ... and so on
 
P

Paul Lutus

cadsjo said:
Hi,

I want to read pieces from a RandomAccessFile
1 piece = a name (25 chars)
1 piece = a nr (7 chars)

Help me!
I don't know how to start and go further...

Thanks! John

My file looks like this
name a 1234567name b 7654321name c ... and so
on

This is called a fixed-field-width database. I hope the data are not all on
one long line as you show it.

1. Read lines from the file.

2. Split the lines up using String.substring()

If the file really is one long line with no line breaks, then:

1. Read characters in groups based on the known length of the records.

2. Create a String out of the read groups.

3. Split the Strings as above.
 
W

Will Hartung

Paul Lutus said:
This is called a fixed-field-width database. I hope the data are not all on
one long line as you show it.

*sigh*

Why not? It's, like you said, a fixed field width database. Lines are
irrelevant.

Given the "schema" you presented, you have records that are 32 (25 + 7)
bytes (crass assumption, but realistic) long.

So, to read a record from this file, you would open a RandomAccessFile,
calculate the offset using the record length, RandomAccessFile.seek() to
that position, and then read record length bytes into a byte array used as a
record buffer.

Finally, you'd break the record buffer up into fields with which you'd do
with what you please.

Crude example:
public byte[] readRecord(int recordNum)
{
RandomAccessFile f = new RandomAccessFile("file.dat");
long recordLength = 25 + 7;
byte recordBuffer = new byte[recordLength];

long recordOffset = recordNum * recordLength;
f.seek(recordOffset);
f.read(recordBuffer);
f.close();
return recordBuffer;
}

Making this efficient and robust is left as an additional homework
assignment on top of this one.

Regards,

Will Hartung
([email protected])
 
C

cadsjo

Thanks...

This was exactly what i was looking for..!!



Will Hartung said:
Paul Lutus said:
This is called a fixed-field-width database. I hope the data are not all on
one long line as you show it.

*sigh*

Why not? It's, like you said, a fixed field width database. Lines are
irrelevant.

Given the "schema" you presented, you have records that are 32 (25 + 7)
bytes (crass assumption, but realistic) long.

So, to read a record from this file, you would open a RandomAccessFile,
calculate the offset using the record length, RandomAccessFile.seek() to
that position, and then read record length bytes into a byte array used as a
record buffer.

Finally, you'd break the record buffer up into fields with which you'd do
with what you please.

Crude example:
public byte[] readRecord(int recordNum)
{
RandomAccessFile f = new RandomAccessFile("file.dat");
long recordLength = 25 + 7;
byte recordBuffer = new byte[recordLength];

long recordOffset = recordNum * recordLength;
f.seek(recordOffset);
f.read(recordBuffer);
f.close();
return recordBuffer;
}

Making this efficient and robust is left as an additional homework
assignment on top of this one.

Regards,

Will Hartung
([email protected])
 
P

Paul Lutus

Will said:

Whgat? The post shows that the lines are run together without benefit of
linefeeds. But this may result from how the OP copied the text. It is an
entirely appropriate question.
Why not? It's, like you said, a fixed field width database. Lines are
irrelevant.

Lines are not irrelevant. If there are linefeeds, the entire project is
simpler. Records are delimited by linefeeds, fields by whitespace. If this
is not true, the data parsing must be handled differently.
Given the "schema" you presented, you have records that are 32 (25 + 7)
bytes (crass assumption, but realistic) long.

So, to read a record from this file, you would open a RandomAccessFile,
calculate the offset using the record length, RandomAccessFile.seek() to
that position, and then read record length bytes into a byte array used as
a record buffer.

Try opening and reading a text file that may or may not have linefeeds,
using a RandomAccessFile instance, where the files on different platforms
have different line endings. In a case like this, one must know whether
there are or are not linefeeds, and choose a method on that basis.

In any case, it is always simpler and more reliable to read the entire file
sequentially than to use random access. The latter approach is only
appropriate for applications that actually need random access to records.
That is not needed here.
 

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,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top