Hello everyone...
This is my first post and its a doozie...
I am trying to write a object to file (which I think works) and then read the object in from that file.
The problem is that the variables dont seem to be reading in as well...
These are the functions I am using...
Thank you in advance for the help...Code:@Override void writeToFile(String filename)//Writes the object to a file { FileOutputStream fos = null; ObjectOutputStream out = null; try { fos = new FileOutputStream(filename); out = new ObjectOutputStream(fos); out.writeObject(this); out.close(); } catch(IOException ex) { ex.printStackTrace(); } } @Override void readFromFile(String filename) { try { InputStream file = new FileInputStream( filename ); ObjectInput input = new ObjectInputStream ( file ); try { Circle readShapes = (Circle)input.readObject(); System.out.println("Recovered Shape: " + readShapes.ShapeToString()); } finally { input.close(); } } catch(ClassNotFoundException ex) { System.out.println("Error: " + ex.getMessage()); } catch(IOException ex) { System.out.println("Error: " + ex.getMessage()); } }![]()
Last edited by ZekeDragon; 03-15-2010 at 06:33 AM. Reason: Please use [code] tags around program code.
To write an object into a file, the object needs to implement Serializable class.
Are you implementing that?
Thanks for the reply, but I did implement the Serializable class...
Just figured I won't paste my whole class in here...
All that goes wrong is that variables are not read or written properly... For instance I have a variable called x... When I write the object to the file it has a value 3... Then when it reads the object in again, it has a value 0...
This is the only part I'm having trouble with...
Once again thanks for your fast reply ^_^
Yeah, i realized that you probably did implement that, otherwise you probably would've had some exception or something.
I see that you are not doing anything with the object you get from the file... you are only printing it once, but you are not returning it from the method or putting it anywhere, maybe it's something with that...
This "readShapes" field will be lost as soon as the method ends..
Circle readShapes = (Circle)input.readObject();
Because of this line : System.out.println("Recovered Shape: " + readShapes.ShapeToString());
I don't really have to return anything... Its just there to show that it did read the object in. But it doesn't even print out the right answer there... which is strange...
I've never had this problem before... >_< really frustrating
Okay, i understand.
You can try out.flush() before you close the session, when writing.
Also, try printing the object before you write it, to be sure it's not something else's fault.
(Btw, just a little note about the ShapeToString method. You should rename it to toString() and override the object's original method, then you don't need to write the method's name when printing, as System.out.println() always calls object's toString() method automatically.)
1) Try what sinipull said - use flush after writing and only then close the session.
2) If that doesn't work, I think the cause might the fact that you're doing downcasting when converting Object to Circle. Tryand then printing it. I remember that I had a very similar problem when I was doing my assignment and I had to use serialization - I read the object and then tried to compare it to the original by using 'equals'. for some reason it didn't work. What I did, is comparing the two objects by first converting them to strings and applying equals on strings. Anyway, my point is try converting it to string right away and then testing it.Code:Object readShapes = input.readObject();
3) If 1+2 don't work, post your whole code, maybe the problem is somewhere else.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks