Java保存并加载程序的状态
在我的java项目中,我有几个类/java文件,但在存储所有所用内容列表的菜单类中。在数据方面,我存储了6个Lists2 ArrayList和4个HashMaps,其中1个在菜单类中定义,其他的在不同的类中。 因此,我需要创建一个savestate和一个loadstate,以便在关闭程序时恢复以前的状态。所有列表都是用Serializable实现的 是否可以保存所有菜单的状态并重新加载,或者我必须单独保存所有列表?将所有内容保存在一个文件中会很好 这是我的函数,没有警告/错误,编译但不创建文件数据文件 有什么想法吗Java保存并加载程序的状态,java,swing,savestate,Java,Swing,Savestate,在我的java项目中,我有几个类/java文件,但在存储所有所用内容列表的菜单类中。在数据方面,我存储了6个Lists2 ArrayList和4个HashMaps,其中1个在菜单类中定义,其他的在不同的类中。 因此,我需要创建一个savestate和一个loadstate,以便在关闭程序时恢复以前的状态。所有列表都是用Serializable实现的 是否可以保存所有菜单的状态并重新加载,或者我必须单独保存所有列表?将所有内容保存在一个文件中会很好 这是我的函数,没有警告/错误,编译但不创建文件数
private void MenuSave(){
String wd = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(wd);
int rc = fc.showDialog(null, "Select Data File Location to Save");
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String filename = file.getAbsolutePath();
savestate(lf, lc, lv, lcl,filename);}
}
public void savestate(Cars la, Bikes l2, Motos l3, Planes l4, People n1, Food n2, String filename){
int i;
File out = new File(filename);
ObjectOutputStream output = null;
try{
output = new ObjectOutputStream(new FileOutputStream(filename));
for(Car c : la.getCars().values()){
output.writeObject(c);
}
for(Bike b : l2.getBikes().values()){
output.writeObject(b);
}
for(Moto m : l3.getMotos().values()){
output.writeObject(m);
}
for(i=0;i<n1.size();i++)
{output.writeObject(n1.get(i)));
}
for(i=0;i<n2.size();i++)
{output.writeObject(n2.get(i)));
}
}catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
不创建文件datafiles
我敢打赌是的,只是不在你期望的地方。不要把你的文件放在任何地方,把它们放在可读写、合乎逻辑和可复制的地方
String filename = "datafiles";
File out = new File(System.getProperty("user.home"), filename);
// ...
output = new ObjectOutputStream(new FileOutputStream(out));
然后在用户主页中查找数据文件,为什么它没有文件类型/扩展名?文件
为操作系统使用正确的文件分隔符。
user.home是指向具有读/写访问权限的稳定、可复制路径的。
所以,正如我所想的,我只需要单独保存列表,而不必为。 1-选择保存文件的位置,然后将类保存在其中。 2-要读取,只需解析输入并存储替换当前类。 阅读也是一样的:
String wd = System.getProperty("user.dir");
this.setAlwaysOnTop(false);
JFileChooser fc = new JFileChooser(wd);
fc.setDialogType((int)JFileChooser.OPEN_DIALOG);
int rc = fc.showDialog(null, "Select Data File to Load");
this.setAlwaysOnTop(true);
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String filename = file.getAbsolutePath();
ObjectInputStream input = null;
try{
input = new ObjectInputStream(new FileInputStream(file));
this.list1=(ListType1)input.readObject();
this.list2=(ListType2input.readObject();
....
}catch (IOException x){
...
}catch(ClassNotFoundException x){
...
}
}
您可能希望在代码中加入println语句,以查看调用了什么代码以及调用时变量的状态。为什么不使用java.util.Preferences?@trashgod Preferences可能是更好的选择,尽管我们还不知道如何确定。请参阅下面我对mKorbel的评论@trashgod@mKorbel我认为首选项纯粹是字符串或“简单”对象类型Long/Float的值,而不是ObjectOutputStream所暗示的通用对象值。也许OP可以将每个对象的属性存储为这些基本类型。OP需要更多的输入-类的定义将非常有用。@AndrewThompson:我喜欢user.home,但首选项可以存储复杂的类型;更多。这样,它确实会在用户文件夹中创建一个4字节的文件。但是有一个问题,它只会在所有列表都为空时创建,如果我向列表中添加一个元素,它会给出一个异常NullPointerException。然后我需要处理负载。。。
String wd = System.getProperty("user.dir");
this.setAlwaysOnTop(false);
JFileChooser fc = new JFileChooser(wd);
fc.setDialogType((int)JFileChooser.OPEN_DIALOG);
int rc = fc.showDialog(null, "Select Data File to Load");
this.setAlwaysOnTop(true);
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String filename = file.getAbsolutePath();
ObjectInputStream input = null;
try{
input = new ObjectInputStream(new FileInputStream(file));
this.list1=(ListType1)input.readObject();
this.list2=(ListType2input.readObject();
....
}catch (IOException x){
...
}catch(ClassNotFoundException x){
...
}
}