my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes ObjectStreams... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ObjectStreams..." Watch "ObjectStreams..." New topic
Author

ObjectStreams...

Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
hi everybody..
i know that we can read any object with Object Streams by..
ObjectRead() and ObjectWrite()....
when ever we issue ObjectRead() it will give us next object that we have written....is there is any way by which i can directly point to lets say 10th object..
also if i want to modify the object data that i have written in the file how to do that..
please poste its urgent
Thanks
Amit


Amit
University of Florida
luc comeau
Ranch Hand

Joined: Jan 20, 2005
Posts: 97
i may be off on this since im not a java guru, but i would assume you would have to loop on readObject(), until you reach the object your looking for.As for modifying it, once you have the object and cast it to the particular object type it is, you can then use the methods of that class on that object you read. To modify it in the file you have to write the modified object back.

I don't know much about random access files, maybe someone else can help in that area to see if you can do this "un sequentially".But there probably more then likly is a way to do it like this, i just happen to not know it


National Research Council<br />Internet Logic Department
Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
sir its right..
but i want ot write object back to its same position..
means if i want to update 10th object then after modifying i want ot write it back to 10th position..
is the solution provided by u work for that....
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No, there's no provision for doing that; you have to rewrite the whole file. If you have a "database" of separate serialized objects and want to be able to modify them separately, then keep them in separate files.


[Jess in Action][AskingGoodQuestions]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I am affraid that it is not absolutely correct.


You could use java.io.RandomAccessFile to store your objects. Of course, as java.io.RandomAccesFile is based on java.io.DataInput and java.io.DataOutput, and not in java.io.ObjectInput and java.io.ObjectOutput as the java.io.ObjectInputStream and the java.io.ObjectOutputStream does, you will have first to convert your object to bytes.

I did not have much of time to implement decent functionality, but I wrote a few lines of code that shows how you could read an index-object in a a RandomAccessFile.

This is the strategy I used
  • Convert your objects to byte arrays using a ObjectOutputStream writing into a ByteArrayOutputStream.
  • Store references to the byte arrays in an java.util.List
  • Save into a file using DataOutputStream the metadata containing the number of records, that's to say, the number of objects to be serialized, and the size in bytes of every record.
  • Then, using the same DataOutputStream write every byte array into the stream
  • Then using the RandoAccessFile read the metadata, first the number of records, then the size of every record.
  • With this data calculate the metadata offset
  • Then calculata the position of any object on the file.
  • Visit that position with the java.io.RandomAccessFile.seek() method
  • Read the byte array and use it in a ByteArrayInputStream
  • Finally use the ByteArrayInputStream as the source for the ObjectInputStream and read the object.


  • Once again, this is not decent code, this is just to show how to do it.

    Regards,
    Edwin Dalorzo


    My Output is:
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Originally posted by Edwin Dalorzo:
    I am affraid that it is not absolutely correct.


    You've shown how to store multiple objects in a file, and change single objects. This will work, by the way, only if the changes don't change the size of the data, or introduce any new objects. Changing the values of primitive fields should generally work, though. I recommended separate files instead of a single RandomAccessFile because it's a more general solution.

    But in any case, that's not what the OP asked: he asked how to change a given object as read from a single serialized data stream, such that if the Stream is read again, one object would be different.
    Edwin Dalorzo
    Ranch Hand

    Joined: Dec 31, 2004
    Posts: 961

    But in any case, that's not what the OP asked: he asked how to change a given object as read from a single serialized data stream, such that if the Stream is read again, one object would be different.


    From that poing of view, Ernest, comrade, You are completely right!

    I, likewise, think it is not possible and you're right, maybe writing multiple objects is a best approach, maybe reading them altogether with a java.io.SequenceInputStream if you want to reload them all, or just the file you want, to reload a particular Object.

    Does anyone knows how to serialize objects to XML? Maybe that way we could repleace a particular node in the XML file, and that'll be much easier then changing a RandomAccessFile. Do you not think so?

    I think there is something about the subject on
    [url=http://xstream.codehaus.org]Code House[/ulr]
    [ January 13, 2006: Message edited by: Edwin Dalorzo ]
     
    Don't get me started about those stupid light bulbs.
     
    subject: ObjectStreams...