gpt4 book ai didi

java gui - 保存和加载数据

转载 作者:行者123 更新时间:2023-12-02 00:26:17 24 4
gpt4 key购买 nike

我有一个类库,看起来像这样:

public class Library implements Serializable{


private static final long serialVersionUID = 1L;
private static Library library; //a library
private static Recommender recommender; //an instance of the recommender class
private Set<LibraryItem> items; //set of library items
private Set<Reader> readers; //set of readers
private Set<Author> authors; //set of authors
private Set<LibraryCollection> collections; //set of library collections
private Set<Librarian> librarians; //set of the library librarians

库类是一个单例类。我构建 GUI 窗口来表示类中的不同方法,例如向库添加阅读器。

在主类中,我有以下代码来保存和加载库:

public class MainClass implements Serializable{


private static final long serialVersionUID = 1L;
public static Library library;

public static void main(String[] args)
{
try {
library = loading();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

if(library == null) {
library = Library.getInstance();
}
}
public static Library loading() throws IOException //loading the system object
{
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("librery.srl"));
library = (Library)in.readObject();
JOptionPane.showMessageDialog(null,"Loading librery.ser file to the database", "Loading File", JOptionPane.INFORMATION_MESSAGE);
return library;
}

//in case the file is not found
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"File wasn't found, creating new library:", "Missing file", JOptionPane.ERROR_MESSAGE);
return library;

}

//in case there was a problem loading data from file
catch(IOException eio)
{
JOptionPane.showMessageDialog(null,"system wasn't able to read from file.. creating new library", "Read File Error", JOptionPane.INFORMATION_MESSAGE);
return library;
}

//genreal exceptions
catch (Exception e)
{
System.out.println(e.getMessage());
return library;
}
}//end of loading method

public static void save(Library library) throws IOException
{
//trying to save the data
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("librery.srl"));
out.writeObject(library);
out.close();
}

//general exceptions
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Saving file was failed", "Save Error", JOptionPane.ERROR_MESSAGE);
}
}//end of save method

}

我以这种方式保存库:

public class SaveLibrary extends JFrame {

private static final long serialVersionUID = 1L;

public SaveLibrary() {
super("Save Library");
JLabel lblDoYouWant = new JLabel("Do you want to save the library?");
lblDoYouWant.setIcon(new ImageIcon("icons/icon.png"));
lblDoYouWant.setBounds(0, 0, 434, 22);
lblDoYouWant.setVerticalAlignment(SwingConstants.BOTTOM);
lblDoYouWant.setHorizontalAlignment(SwingConstants.LEFT);
lblDoYouWant.setFont(new Font("Tahoma", Font.BOLD, 18));
lblDoYouWant.setBackground(new Color(216, 191, 216));
getContentPane().add(lblDoYouWant);

JRadioButton rdbtnYes = new JRadioButton("Yes");
rdbtnYes.setIcon(new ImageIcon("icons/yes.png"));
rdbtnYes.setHorizontalAlignment(SwingConstants.LEFT);
rdbtnYes.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
//if the button is selected save the library and exit the program
try {
MainClass.save(MainClass.library);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
});
rdbtnYes.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
rdbtnYes.setBackground(new Color(216, 191, 216));
rdbtnYes.setBounds(0, 48, 109, 23);
getContentPane().add(rdbtnYes);
}

当我运行该程序时,它不会将一次运行的数据保存到另一次运行。我的保存和加载方法是否需要更改?我只是不明白为什么它不起作用。

最佳答案

如果您想序列化 Library,那么 Recommender、LibraryItem...也必须是可序列化的。为了控制事物的序列化方式,请考虑使用外部化而不是序列化。

关于java gui - 保存和加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com