我使用Gson
序列化class
稱爲User.class
延伸Externalizable
外部化:序列化與GSON
public class User implements Externalizable {
String username = "testname";
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(username);
}
}
文件中的輸出是
{
user: {}
}
,而不是
{
user: "testname"
}
你如何序列化對象?如果它是一個簡單的pojo,只需爲要序列化的屬性添加getter和setter即可。無需實現Externalizable。 – Ulises