Jump to content




Recent Topics

Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Object reading and writing...


  • Please log in to reply
6 replies to this topic

#1 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 11 March 2010 - 11:50 AM

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


@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());
        }
    }

Thank you in advance for the help... :pinguin:

Edited by ZekeDragon, 15 March 2010 - 05:33 AM.
Please use [code] tags around program code.

  • 0

#2 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 11 March 2010 - 12:10 PM

To write an object into a file, the object needs to implement Serializable class.
Are you implementing that?
  • 0

#3 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 11 March 2010 - 12:16 PM

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 ^_^
  • 0

#4 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 11 March 2010 - 12:22 PM

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();
  • 0

#5 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 11 March 2010 - 12:31 PM

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
  • 0

#6 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 383 posts

Posted 11 March 2010 - 12:38 PM

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.)
  • 0

#7 bobdark

bobdark

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 155 posts

Posted 11 March 2010 - 01:26 PM

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. Try
Object readShapes = input.readObject();
and 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.
3) If 1+2 don't work, post your whole code, maybe the problem is somewhere else.
  • 0