我正在尝试从文件中读取所有对象。下面是用于从文件中获取对象列表的方法的片段。
DELETE FROM
YourTable
WHERE
AID NOT IN ( SELECT
MAX(AID)
FROM
YourTable
GROUP BY
BID ,
AName ,
AType ,
APrice );
RegisterParty Class:
public List displayParties() {
ObjectInputStream ois = null;
List<RegisterParty> results = new ArrayList();
try {
FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
ois = new ObjectInputStream(fis);
while (true) {
System.out.println(" inside while true");
results.add((RegisterParty) ois.readObject());
System.out.println(results);
}
} catch (IOException ex) {
ex.printStackTrace();
}
catch (Exception ex){
ex.printStackTrace();
} finally{
try {
ois.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
return results;
}
但得到以下异常:
public class RegisterParty implements Serializable {
String bookingPersonName;
String childName;
String childAge;
String theme;
String foodAlergies;
String noOfGuest;
String specialGuest;
public String getBookingPersonName() {
return bookingPersonName;
}
public void setBookingPersonName(String bookingPersonName) {
this.bookingPersonName = bookingPersonName;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public String getChildAge() {
return childAge;
}
public void setChildAge(String childAge) {
this.childAge = childAge;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getFoodAlergies() {
return foodAlergies;
}
public void setFoodAlergies(String foodAlergies) {
this.foodAlergies = foodAlergies;
}
public String getNoOfGuest() {
return noOfGuest;
}
public void setNoOfGuest(String noOfGuest) {
this.noOfGuest = noOfGuest;
}
public String getSpecialGuest() {
return specialGuest;
}
public void setSpecialGuest(String specialGuest) {
this.specialGuest = specialGuest;
}
public String toString(){
return bookingPersonName+" "+childName+" "
+childAge+" "+foodAlergies+" "+theme+" "+noOfGuest+" "+specialGuest;
}
}
RegisterParty实现了可序列化的接口。它是一个基于菜单的应用程序,所以当我将对象保存在文件中时,它的保存成功。但是调用方法来获取列表中的所有对象,它抛出异常。任何想法为什么?
此方法成功执行以序列化对象:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: models.RegisterParty
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at helper.HelperFunctions.displayParties(HelperFunctions.java:39)
at services.PartyOperations.listAllParties(PartyOperations.java:64)
at assignment.App.main(App.java:34)
at helper.HelperFunctions.saveParty(HelperFunctions.java:24)
at services.PartyOperations.registerParty(PartyOperations.java:53)
at assignment.App.main(App.java:28)
Caused by: java.io.NotSerializableException: models.RegisterParty
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at helper.HelperFunctions.saveParty(HelperFunctions.java:20)
at services.PartyOperations.registerParty(PartyOperations.java:52)
... 1 more
答案 0 :(得分:0)
当我实现Serializable对象时,我在类中添加了一个静态变量serialVersionUID:
.product {
z-index:1000;
}
此行被eclipse或android studio添加为建议。我不知道如何使用IntelliJIdea。
值得一试!我相信这个ID用于反序列化。