【发布时间】:2013-10-19 18:31:16
【问题描述】:
所以我有这个对象的数组列表,我想以二进制格式存储到 dat 文件中。
objectA 类有这个构造函数
public ObjectA(String aString,int aNumber ,String[] sampleA){
this.aString = aString;
this.aNumber = aNumber;
this.sampleA= sampleA;
}
在另一个类文件中,我在该类上实例化了它,并用 ObjectA 对象填充了数组列表
private static ArrayList<ObjectA> objectA = new ArrayList<ObjectA>();
我有这个方法
private static void createFile() {
System.out.println("Creating file...");
try {
FileOutputStream fileOut = new FileOutputStream("file.dat");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
for (ObjectA o : objectA) {
objectOut.writeObject(o); //write the object
}
objectOut.close(); //then close the writer
}CATCH EXCEPTIONS***************{}
}
但是我在尝试运行它时发现了这个错误 createFile() 出错! java.io.NotSerializableException:trycatch 中的 ObjectA 有什么想法吗?
【问题讨论】:
标签: java serialization arraylist