Results 1 to 4 of 4
- 04-13-2009, 05:56 PM #1
[SOLVED] Deserialization question
I have some simple code I've been playing with to fix my understanding of serialization and I've managed to be able to serialize and deserialize multiple objects to/from a text file but the issue I'm having is trying to deserialize the objects within the file without having to first re-write them. I know the problem is my loop is using a variable from SerializableObject that gets reset to 0 each time I run the program but I can't figure a way to deserialize every object unless I know this number.
//taken from my SerializeDriver classJava Code:public class SerializableObject implements Serializable{ private static final long serialVersionUID = 1654834268554L; static File file = new File("testSerialize.txt"); static FileOutputStream fos; static ObjectOutputStream oos; private String name; private int age; static int count = 0; static{ try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { System.out.println("File not found exception"); } try { oos = new ObjectOutputStream(fos); } catch (IOException e) { System.out.println("object stream error"); } } public SerializableObject(String n, int a){ this.name = n; this.age = a; count++; try { oos.writeObject(this); System.out.println("Object written"); } catch (IOException e) { System.out.println("Error writing object"); } }
now everything works fine and dandy when I uncomment a call to defaultCreateObjects but I get nothing when all I run is the above readObjects method.Java Code:private static void readObjects() { SerializableObject obj1 = null; try { fis = new FileInputStream(file); System.out.println("fis"); } catch (FileNotFoundException e) { System.out.println("Error extracting file"); } try { ois = new ObjectInputStream(fis); System.out.println("ois"); } catch (IOException e) { System.out.println("InputStream error"); } try { for(int x = 1; x <= SerializableObject.count; x++){ obj1 = (SerializableObject)ois.readObject(); System.out.println(obj1.getName()); } } catch (EOFException e) { System.out.println("Reached end of file"); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-13-2009, 10:24 PM #2
I did some research and after you write your first object, you have to append the output file...
Check out this thread:
Serialization - How to Serialize multiple objects ?Who Cares... As Long As It Works...
- 04-13-2009, 10:41 PM #3
Such a simple fix. I tried doing this earlier but was was trying to append File not FileOutputStream (I blame it on being Monday).
It's now working how I intended it to work 3 months ago when I set this project aside. Now to just move it from the practice code into the actual project and to clean up the explosion of try/catch blocks that's going on.
Thanks for finding that for me. I never had much luck on the sun forums sadly :(Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-14-2009, 12:15 AM #4
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Display and Edit JTable after Deserialization
By grottman in forum New To JavaReplies: 2Last Post: 04-11-2009, 04:26 PM -
help me in dis question
By krishan in forum New To JavaReplies: 10Last Post: 02-16-2009, 07:04 PM -
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks