0

ive got a problem with deserializing at the beginning of the program.

public static void main(String[] args) throws IOException, ClassNotFoundException{

        Test object = new Test(); // Test implements Serializable

        //start
        deserialize();  

        //do something

        //end
        serialize(object);         
    }

public static void deserialize()
    {
        test object = null;

        try
          {
             FileInputStream file= new FileInputStream(".../Example.ser");
             if(file.read()!=-1) //the first time the file will be empty
             {
                 ObjectInputStream read= new ObjectInputStream(file); //here an exception is thrown the second time the program is started
                 object  = (Test) read.readObject();
                 object .printdata();
                 read.close();
                 file.close();
             }
             else
             {
                 file.close(); 
             }
          }catch(IOException i)
          {
             i.printStackTrace();
             return;
          }catch(ClassNotFoundException c)
          {
             c.printStackTrace();
             return;
          } 
    }

    public static void serialize(Test object)
    {   
        try
        {
           FileOutputStream file =
           new FileOutputStream(".../Example.ser");
           ObjectOutputStream write = new ObjectOutputStream(file );
           write .writeObject(object);
           write .close();
           file .close();
        }catch(IOException i)
        {
            i.printStackTrace();
        }
    }

the program works if would switch serialize and deserialize or if i call deserialize after serialize. it runs fine the first time but if i start it a second time deserialize @ ObjectInputStream read= new ObjectInputStream(file); throws an streamcorrupted exception.

at program start the serialize file has to be deserialized and printed and as i said if switch the calls and then copy the call of deserialize back to the top it works but not if it stays like that. the first time it runs but the second time the exception is thrown.

6
  • Test Class don't have any variables to store in the .ser file? Have you initialised them in Test class itself?. what is the value you have in the object that was put for serialization Commented Jan 14, 2015 at 9:59
  • Try FileOutputStream file = new FileOutputStream(".../Example.ser",false); when you serialize to empty the file. Commented Jan 14, 2015 at 10:05
  • Do you have the .ser file generated on that path. you will get streamcorrupted exception when you dont have that .ser file Commented Jan 14, 2015 at 10:07
  • 1
    @KalaiarasanManimaran No, he will get FileNotFoundException if the file doesn't exist. You can't get any kind a stream whether corrupt or otherwise from a non-existent file. Commented Jan 14, 2015 at 10:38
  • @PabloGallegoFalcón new FileOutputStream(String) is equivalent to new FileOutputStream(String, boolean) when the second parameter is false. Commented Jan 14, 2015 at 10:39

1 Answer 1

2
if(file.read()!=-1) //the first time the file will be empty

The problem is here. You are reading and throwing away the first byte of the file, so the following reads will be out of sync. Remove it. The comment isn't correct either. The first time you run this code, the file will be absent, not empty. If you still need to test for a zero-length file, just catch EOFException separately, as you're only reading one object.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks now the error is gone but the serializing is not working now. it seems when i start the program again, the values are overwritten and the old ones are gone.
You'll need to start a new question for that.
cant that be answered here? why do i have to create a new question
You're not suppose to ask multiple questions and you're certainly not supposed to ask new questions in comments. You're also supposed to upvote and/or accept answers that you find useful. It's not a forum, or a free help desk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.