Home arrow static arrow Java Programming [Archive] - "Class object" down to file?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - "Class object" down to file?
This topic has 5 replies on 1 page.

Posts:8
Registered: 7/28/04
"Class object" down to file?  
Aug 5, 2004 3:48 AM



 
Hi all!

I have a nice problem that I feel should be easily solved, but how?!

I have a class "MyStruct", which is a struct for storing things. I create an instance of MyStruct and want to store it in i file (I do this through converting it to a String. Wrong?). Then I retrieve the object from the file convert it back to a MyStruct. I realize there may be far more simple ways of doing this than I've solved it... can anyone help?

What I did, and which does not work, is firstly this:

// create the struct:
MyStruct struct = new Struct (test, string);
// Open a file and write a String converted MyStruct to it:
DataOutputStream out = new DataOutputStream (new FileOutputStream ("Saved.txt"));
out.writeBytes (struct.toString ());
out.close ();

/// and then ///:

// Open the file
FileInputStream fin = new FileInputStream("Latest.txt");
BufferedReader myInput = new BufferedReader (new InputStreamReader(fin));
// read lines (String) if there exist any
while ((thisLine = myInput.readLine()) != null)
{
// convert the String back to an object:
Object obj = (Object) thisLine;
// then try to convert the Object to a MyStruct:
MyStruct struct2 = (MyStruct) obj; *********** it is this cast that does not work!!!!!
}
 

Posts:5,965
Registered: 5/17/03
Re: "Class object" down to file?  
Aug 5, 2004 4:08 AM (reply 1 of 5)



 
Can't you use ObjectOutputStream/ObjectInputStream.
 

Posts:5,965
Registered: 5/17/03
Re: "Class object" down to file?  
Aug 5, 2004 4:09 AM (reply 2 of 5)



 
Examples of serialization/deserialization,

http://javaalmanac.com/egs/java.io/SerializeObj.html
 

Posts:8
Registered: 7/28/04
Re: "Class object" down to file?  
Aug 8, 2004 11:37 PM (reply 3 of 5)



 
The link and advice helped me thanx!!

If anyone else has the same problem, this is how it was done correctly:

/* Write the object to a file: "Latest.ser" */
try {
ObjectOutput out = new ObjectOutputStream (new FileOutputStream ("Latest.ser"));
out.writeObject (state); // state = instance of the State class which is a homemade simple struct class
}
catch (IOException e) {}

/* Read from the file */
try {
File file = new File ("Latest.ser");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));

readState = (State) in.readObject();
in.close();
}
catch (Exception e) {}
 

Posts:3,183
Registered: 10.09.03
Re: "Class object" down to file?  
Aug 8, 2004 11:53 PM (reply 4 of 5)



 
Yeah that's the quick solution, but also the dirty one. I believe the best solution is to provide the output yourself. For example I provide methods for dumping and reading the object from and to XML. It's more work, but a lot cleaner.
 

Posts:964
Registered: 7/23/03
Re: "Class object" down to file?  
Aug 9, 2004 12:01 AM (reply 5 of 5)



 
Yeah that's the quick solution, but also the dirty
one. I believe the best solution is to provide the
output yourself. For example I provide methods for
dumping and reading the object from and to XML. It's
more work, but a lot cleaner.

just want to know.... will parsing xml and reconstruct the object slower then deserialize a object ? (especilly for a large object) ?
 
This topic has 5 replies on 1 page.