Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2009, 06:56 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.

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
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-13-2009, 11:24 PM
markw8500's Avatar
Senior Member
 
Join Date: Jul 2008
Location: Pennsylvania, USA
Posts: 134
Rep Power: 0
markw8500 is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-13-2009, 11:41 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-14-2009, 01:15 AM
markw8500's Avatar
Senior Member
 
Join Date: Jul 2008
Location: Pennsylvania, USA
Posts: 134
Rep Power: 0
markw8500 is on a distinguished road
Default
No Prob...
__________________
Who Cares... As Long As It Works...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display and Edit JTable after Deserialization grottman New To Java 2 04-11-2009 05:26 PM
help me in dis question krishan New To Java 10 02-16-2009 08:04 PM
Serialization/Deserialization Error andrepezzo Advanced Java 2 12-16-2008 06:36 PM
Serialization/Deserialization Error andrepezzo Networking 0 12-16-2008 05:21 PM
Question mark colon operator question orchid Advanced Java 8 11-27-2008 07:36 AM


All times are GMT +2. The time now is 06:39 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org