Saving to binary/serialization java

后端 未结 2 366
离开以前
离开以前 2021-01-15 08:32

I have to, quote on quote,

1.Save accounts to a binary (serialized) file. 2.Load (recreate) accounts from a binary (serialized) file.

相关标签:
2条回答
  • 2021-01-15 09:03

    Assuming you have a class called "account" you simply need to implements Serializable at the top of your class header.

    From my understanding, that will serialize all the data into a binary form. You will need to of course still perform the steps to actually write/read the class object out to a file using ObjectOutputStream/ObjectInputStream.

    So for example...

    public class account implements Serializable
    { ...
    }
    

    Then in your main function for example, where you want to save the object, you would create a File, attach it to an ObjectOutputStream and write out your object in binary form.

    0 讨论(0)
  • 2021-01-15 09:08

    First hit on google: http://www.javacoffeebreak.com/articles/serialization/index.html - basically you should serialize your object to a file. Then you can load it into an object again later.

    0 讨论(0)
提交回复
热议问题