Closed Thread
Results 1 to 7 of 7

Thread: Object reading and writing...

  1. #1
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Object reading and writing...

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


    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());
            }
        }
    Thank you in advance for the help...
    Last edited by ZekeDragon; 03-15-2010 at 06:33 AM. Reason: Please use [code] tags around program code.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Object reading and writing...

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

  4. #3
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: Object reading and writing...

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

  5. #4
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Object reading and writing...

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

  6. #5
    lintwurm's Avatar
    lintwurm is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Pretoria
    Posts
    81
    Rep Power
    0

    Re: Object reading and writing...

    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

  7. #6
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Object reading and writing...

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

  8. #7
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Object reading and writing...

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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Writing an object to file{help}
    By hbk in forum Managed C++
    Replies: 1
    Last Post: 01-02-2011, 12:08 PM
  2. First writing and then reading, reopen ?
    By denarced in forum C and C++
    Replies: 2
    Last Post: 10-26-2010, 08:46 PM
  3. Reading and Writing Files in C
    By Guest in forum C Tutorials
    Replies: 39
    Last Post: 06-20-2010, 03:08 PM
  4. Dropdown writing to DB, but not reading
    By blink41 in forum PHP Development
    Replies: 9
    Last Post: 04-29-2010, 06:24 AM
  5. Reading / Writing Objects
    By ahmed in forum C# Programming
    Replies: 1
    Last Post: 10-08-2009, 06:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

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