简体   繁体   中英

Java Object Serialization issue

I have some input that I add to a serialized object.

Now when I read the serialized object, I want to check if it exists... If not loop till it has a value in it.

How do i modify the deserialization function to handle that.

There is basically a delay in populating my serializable object. So in the meantime if i were to read that object, it is going to be empty. I want to put a check to read only when it has data in it. if not it should wait till it has some data

public String _displayResults(){
String  SomeData = "";
try {
    FileInputStream fis = new FileInputStream("SomeDataobj");
    ObjectInputStream ois = new ObjectInputStream(fis);
    SomeData = (String)ois.readObject();
    ois.close();
}
catch(Exception e) {
    System.out.println("Exception during deserialization: ");
}
return SomeData;
}

What I tried:

added a wait condition for 2 secs for 10 times... Is there a cleaner way.

    while ( ois.readObject().toString().equalsIgnoreCase("") && i <10){
        Thread.sleep(2000);
        i++;
    }

Java provides an API called Externalizable, which allows you to customize the (de) serialization. Serialiazable is marker interface and that indicates the object can be wrote to output stream. Externalizable provides two methods readExternal() and writeExternal() where you can override the behavior.

Your question is not so clear about what you want to achieve, so I am not sure if the above information is helpful for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM