Results 1 to 4 of 4
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default [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.

    Java 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");
    		}
    	}
    //taken from my SerializeDriver class
    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();
    		}		
    	}
    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.
    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.

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    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...

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Display and Edit JTable after Deserialization
    By grottman in forum New To Java
    Replies: 2
    Last Post: 04-11-2009, 05:26 PM
  3. help me in dis question
    By krishan in forum New To Java
    Replies: 10
    Last Post: 02-16-2009, 08:04 PM
  4. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 06:36 PM
  5. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 05:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •