0

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);

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this answer, an object is serializable if it implements Serializable, and all its non-static non-transient fields are serializable or primitives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.