• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrival of data stored as object

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have stored data in object form.How to retrive this data from the file.As serialised objects are stored how to read through diff records and what r the delimitors.
Pl help
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you wrote the object out to the file you use an ObjectOutputStream. Assuming that your object supports serialization, it will be written to the stream. When you read objects back in from a serialized format, you read them using the ObjectInputStream. This will generate an object or class Object. You will have to cast your object back to its original type before being able to use it. Hope that helps
Bodie
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming yourClass has been declared "implements Serializable":
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( "file.dat" ) );
out.writeObject( yourClassObj );
ObjectInputStream in = new ObjectInputStream( new FileInputStream( "file.dat" ) );
newObj = ( yourClass )in.readObject();
Of course both of these would have to be executed in a try/catch block, and you would want to close each stream when you are done with it.
~ Ryan
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah...
Once your object has been read back via readObject(), you can simply access its variables like any other object, assigning them to new working variables if you wish (or doing whatever you want with them):
int newX = newObj.x;
boolean newActive = newObj.active;
et cetera...
 
Aji Ozkan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After getting the object and typecasting how to read the data from that obj???

Originally posted by Ajit Kanada:
I have stored data in object form.How to retrive this data from the file.As serialised objects are stored how to read through diff records and what r the delimitors.
Pl help


 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to either use the getter methods in the object if it has any (like obj.getName()) or you need to look at the variables directly assuming that they are not private.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic