我想将自定义的对象数组保存为文件,并在 Java 程序启动时将其重新读回为对象数组。如果我也可以将其保存为 JSON,那就太好了。我尝试了一些常见的方法,但收到错误消息,指出我的数组不可序列化。
class ArrayOfObjects {
public static void main (String[] args) throws Exception {
Students[] studentArray = new Students[3];
studentArray[0] = new Students();
studentArray[0].age = 18;
studentArray[0].name = "Jones";
studentArray[1] = new Students();
studentArray[1].age = 21;
studentArray[1].name = "David";
studentArray[2] = new Students();
studentArray[2].age = 15;
studentArray[2].name = "Jeremy";
}
}
class Students {
int age;
String name;
}
最佳答案
你的类(class)学生应该实现 Serialiazble
关于java - 将自定义对象数组保存到文件并在java中再次读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450113/