Saving to binary/serialization java

后端 未结 2 374
离开以前
离开以前 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.

提交回复
热议问题