how to write objects to file and record their position?

J

Jeff

I would like to build an index for objects that I write to a file. I can
already write objects, then read them by starting at the beginning of the
file. Now I would like to do add the file position of an object to an
index, then read based on that index.

My problem is that I can't figure out the classes to use.

new ObjectOutputStream( RandomAccessFile ) is not a valid constructor
for ObjectOutputStream

FileOutputStream fs = FileOutputStream( filename);
os = new ObjectOutputStream ( fs );

the above works, but I can't find a way to get the current file position
before calling writeObject.

I'd appreciate any suggestions.

Thanks

Jeff
 
S

Sven Topp

Could use a counter everytime you write to the file?
Then append the counter value to the front of the object when you write it.

Then you could search the file by looking for the appropriate numerical
value at the front of the object. Actually as someone mentioned earlier
in a different thread you might be better off storing the data in plain
text.
Starmage
 
G

Gordon Beaton

I would like to build an index for objects that I write to a file. I
can already write objects, then read them by starting at the
beginning of the file. Now I would like to do add the file position
of an object to an index, then read based on that index.

My problem is that I can't figure out the classes to use.

new ObjectOutputStream( RandomAccessFile ) is not a valid
constructor for ObjectOutputStream

RandomAccessFile is not a Stream. If it were, it wouldn't be random
access.

Wrap your ObjectOutputStream around a ByteArrayOutputStream. Write the
object and store the resulting byte array in the RandomAccessFile.

To restore an object, read the corresponding byte array from the
RandomAccessFile, create a ByteArrayInputStream from the byte array,
then wrap it in an ObjectInputStream to read the object.

You need to create a *new* ObjectOutputStream/ByteArrayOutputStream
for *each* object that you want to be able to retrieve, or you will be
unable to retrieve them independently.

You also need to implement a mechanism for keeping track of the
positions and sizes of each object (actually its byte array) in the
RandomAccessFile, maybe by reserving some space at the beginning of
the file for a table of contents.

Realize that you may run into strange problems if a referred object
occurs in more than one independent section of the RandomAccessFile.
Consider two objects A and B that both refer to a common object C. If
A and B are stored separately and later restored, they'll no longer
refer to the same C unless you make some provision for handling the
situation.

/gordon
 
R

Roedy Green

I would like to build an index for objects that I write to a file. I can
already write objects, then read them by starting at the beginning of the
file. Now I would like to do add the file position of an object to an
index, then read based on that index.

See http://mindprod.com/fileio.html

Tell it you want to read/write objects to a random access file.

The catch is knowing in advance how long objects will be. To solve
that problem see http://mindprod.com/jgloss/hermitcrab.html
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top