我需要将一些Bus对象写入文件
class Bus implements Serializable {
int busno;
int bcount;
int availcount;
String source,destination;
ArrayList<Integer> booked = new ArrayList<Integer>();
HashMap<Integer,Customer> passtick = new HashMap<>();
Bus(int b,String src,String des)
{
busno = b;
bcount = 0;
availcount = 50;
source = src;
destination = des;
}
一个类维护Bus
es的哈希集
public class Global {
static Set<Bus> buses = new HashSet<Bus>();
}
另一个类具有方法exit()
,该方法将哈希集中的Bus
es写入文件Buses.txt
public void exit() throws Exception{
FileOutputStream fos = new FileOutputStream("Buses.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(Bus b : Global.buses) {
oos.writeObject(b);
}
}
在运行时出现异常
java.io.NotSerializableException: Bus
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Booking.exit(Booking.java:178)
at ClientHandler.run(BusTicketSystem.java:172)
Bus类的实例包含一个ArrayList和一个HashMap。这些会导致序列化问题吗?谢谢。