سؤال

I am developing an application that have some params as config data. I wrote a swing app and declare a class as ConfigClass.I add configuration params fields to this class and store it by serializable implementation to a file.
During read and write, I apply cryption to protect passwords. In future versions of my app I will add or del some fields and want my customer don't loose values. so I tried to use "serialVersionUID=34L" That when my customer upgrade my app, new app can read old config and get old/new common fields. In this way my customer not pushed to reenter data again. Problem is when I del fields and above protocol not works well! why?

public class UserConfiguration implements Serializable  {  
    static final long serialVersionUID = 42L;

    //USER Define User Config elements
    private String bankName;
    private String bankCode;
}
هل كانت مفيدة؟

المحلول

Java serialization is the wrong mechanism to use if you plan on changing the classes between the serialization and deserialization, which can occur when Java serialization is used to store data.

In the case where you delete a field, the serialized representation is expecting that field to be there and when it isn't an error occurs because there's nowhere to write the data.

You should look for alternative mechanisms, such a XML or JSON to store the data.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top