简体   繁体   中英

java - how do you write arraylist to a binary file

how do you write ArrayList into a binary file?

lets say i have a arraylist called people

i am trying to write that into a binary file using writeListToBinary method

my method:

public void writeListToBinary (ArrayList<Person> inList) throws IOException

in my main method:

ArrayList<Person> people = new ArrayList<Person>();
writeListToBinary(people);

I think you are trying to serialize your ArrayList . Implement Serializable to your class, then:

FileOutputStream fos = new FileOutputStream(fileToSaveTo); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(people)

Also take a look at http://www.tutorialspoint.com/java/java_serialization.htm

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