Java序列化没有按我预期的方式工作
我正在尝试使用Java序列化没有按我预期的方式工作,java,serialization,Java,Serialization,我正在尝试使用ObjectOutputStream来序列化ArrayList 以下是处方课程: import java.io.Serializable; import java.util.Calendar; public class Prescription implements Serializable { private static final long serialVersionUID = 4432845389029948144L; private String n
ObjectOutputStream来序列化ArrayList
以下是处方
课程:
import java.io.Serializable;
import java.util.Calendar;
public class Prescription implements Serializable {
private static final long serialVersionUID = 4432845389029948144L;
private String name;
private String dosage;
private int originalQuantity = 0;
private int quantityRemaining = 0;
private String prescribingPharmacy;
private long dateStarted = 0;
private boolean taken_AM = false;
private boolean taken_Noon = false;
private boolean taken_PM = false;
private boolean taken_Mon = false;
private boolean taken_Tue = false;
private boolean taken_Wed = false;
private boolean taken_Thu = false;
private boolean taken_Fri = false;
private boolean taken_Sat = false;
private boolean taken_Sun = false;
public Prescription(){
this.name = "Unknown";
}
public Prescription(String name, String dosage, int quantity, String pharmacy){
this.name = name;
this.dosage = dosage;
this.originalQuantity = quantity;
this.quantityRemaining = quantity;
this.prescribingPharmacy = pharmacy;
this.dateStarted = Calendar.getInstance().getTimeInMillis();
}
public void setTaken(boolean AM, boolean Noon, boolean PM){
this.taken_AM = AM;
this.taken_Noon = Noon;
this.taken_PM = PM;
}
public void setTaken(boolean Mon, boolean Tue, boolean Wed, boolean Thu,
boolean Fri, boolean Sat, boolean Sun){
this.taken_Mon = Mon;
this.taken_Tue = Tue;
this.taken_Wed = Wed;
this.taken_Thu = Thu;
this.taken_Fri = Fri;
this.taken_Sat = Sat;
this.taken_Sun = Sun;
}
public String getName(){
return this.name;
}
}
然而,当我尝试这样做时,我在Prescription
类上得到了一个notserializableeexception
。这对我来说没有任何意义,因为我只使用基本数据类型和String
下面是我用来进行序列化的函数:
public boolean saveToFile(){
try {
FileOutputStream fos = this.context.openFileOutput(LIST_SAVE_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.pList);
oos.close();
} catch(FileNotFoundException e){
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
守则:
public static void main(String[] args) {
try {
List<Prescription> d = new ArrayList<Prescription>();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("C:\\temp\\Prescription.dat")));
d.add(new Prescription());
oos.writeObject(d);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicstaticvoidmain(字符串[]args){
试一试{
列表d=新的ArrayList();
ObjectOutputStream oos=新的ObjectOutputStream(新文件(“C:\\temp\\PRECUMENTION.dat”);
d、 添加(新处方());
oos.writeObject(d);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
你发布的类没有任何异常,所以有件事你没有告诉我们:-)也许序列化代码会有帮助。干净的构建,同样的问题。这个.pList是什么对象?这是ArrayList
不是问题的一部分,但是你真的应该命名settake(…)
和settake(…)
到setTime(…)
和setDays(…)
或类似的东西。如果列表是空的,这有关系吗,因为它在大多数情况下对我来说很可能是空的?成功了,你是对的。我只是想稍后从一个旧的序列化文件中读取需要清除的内容。谢谢