我正在用java創建酒店預訂系統。當我退出程序時,它不保留我以前輸入的信息。有關如何解決此問題的任何建議?非常感謝。以Java保存信息
代碼:
//Creates a method that saves the data in a file
public void save(String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//Creates a method that loads the data from the file
public void load(String filePath) {
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream in = new ObjectInputStream(fis);
ToSave save = (ToSave)in.readObject();
this.setGuests(save.getGuests());
this.setReservations(save.getReservations());
this.setRooms(save.getRooms());
in.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
字符串FILE_PATH = 「HotelReservationSystem.hrs」;
if(clicker == save) {
ToSave save = new ToSave();
save.setGuests(Global.guests);
save.setReservations(Global.reservations);
save.setRooms(Global.rooms);
save.save(FILE_PATH);
}
if(clicker == load) {
ToSave save = new ToSave();
save.load(FILE_PATH);
Global.guests = save.getGuests();
Global.reservations = save.getReservations();
Global.rooms = save.getRooms();
}
所以輸入工作正常,但保留獲取錯誤信息? – dan
您想在程序開始時加載您的信息。程序退出時,內存中不會保存任何內容。 – Joe
當我還在程序中時,它會保存信息。但是,當我再次打開並搜索客人時,該文件爲空。 – user3212307