Save / Load ArrayList class container to a file
in
Share your Work
•
3 years ago
Hello,
wanted to share a solution to save ArrayList to file and read it back to sketch.
If ArrayList contais custom classes (e.g. Zeta), I get java.io.NotSerializableException
when I add implements Serializable into Zeta, it also has the same error.
If I put Zeta into separate Java file and leave it in sketch folder it works fine. (solution from here)
SaveObject and LoadObject are from here.
Alternative solutions?
Zeta.java:
- import java.io.Serializable;
- public class Zeta implements Serializable
- {
- Zeta (){
- }
- public void a()
- {
- System.out.println("Aloha "+Math.random());
- }
- }
saveLoadArrayListToFile.pde:
- ArrayList alfa;
- ArrayList beta;
- void setup()
- {
- alfa=new ArrayList();
- beta=new ArrayList();
- for (int i=0;i<50;i++)
- alfa.add(new Zeta());
- SaveObject("alfatest.bin", alfa); // SAVE
- beta = (ArrayList) LoadObject("alfatest.bin"); // LOAD
- for (int i=0;i<beta.size();i++)
- {
- Zeta temp=(Zeta)beta.get(i);
- temp.a();
- }
- exit();
- }
1