保存正在运行的应用程序
我有一个程序,我需要保存正在运行的应用程序以便稍后能够返回到它 我知道我可以通过写入/读取文本文件来实现此目的,但是该程序非常庞大,因此这并不是一个真正的好方法,因为我有 10 多个类和数千个 JTextFields、JComboBoxs 等。有谁知道我可以在不写入/读取文本文件的情况下实现此目的吗?
我需要做的一个例子是:
在 Microsoft Excel 中,您可以将文件 (.exl) 加载到其中并能够编辑它们。
I have a program where I need to save a running application to be able to go back to it later
I know that I can write/read from a text file to achieve this but the program is pretty prodigious so it's not really a good way to do it because I have 10+ classes and thousands of JTextFields, JComboBoxs, etc. Does anyone know of a way I can achieve this without writing/reading from text files?
An example of what I need to be able to do is this:
In Microsoft Excel you can load files (.exl) into it and be able to edit them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行此操作的一个好方法是将控件中的数据保存为规范形式,然后使该类可序列化。然后您可以将该数据保存到文件中。这是关于 Java 序列化的链接。
更新
我刚刚注意到您说您有数千个表单控件。因此,您可能不想在一个类中完成所有这些操作,但您可能希望维护类的层次结构并将数据拆分为单独的类。这也将帮助您区分您的担忧。希望您有代表您的数据的 POJO 或域类。如果是这样的话,你的任务就会容易得多。这也是为什么分离关注点是好的:)。
A good way to do this is to save the data from your controls into a canonical form and then make that class serializable. You can then persist that data to a file. Here's a link about serialization in Java.
UPDATE
I just noticed that you said you have thousands of form controls. So you probably don't want to do all of this in one class, but you probably want to maintain a hierarchy of classes and split out the data into separate classes. This will also help you separate your concerns. Hopefully you have POJOs or domain classes that represents your data. If that is the case, your task will be much easier. This is also why separating concerns is good :).
为了保存应用程序的状态,我可以想到两种流行的方法:
1)将应用程序的状态保存在数据库中
2) 将应用程序的状态保存在二进制文件或 XML、json 或任何您想要的格式中。
也许会提供有关该应用程序的更多详细信息。会有帮助的。
它是 Web 应用程序、胖客户端应用程序、客户端/服务器应用程序吗...!^
解决方案可能因应用程序类型而异。
希望有帮助。
To save the state of an application, i can think of two popular way:
1) save the state of the application in a Database
2) save the state of the application in a binary file or XML,json or any format you want.
Maybe giving more details about the app. would help.
is it a Web app, fat client app, client/server app... !^
Solution may vary with the type of application.
Hope it help.
您认为 Excel 是如何做到这一点的?它将每个单元格的类型和值以及描述工作表的元数据以其自己专有的二进制格式存储在文件中。如果您有一个具有复杂内部状态的自定义应用程序,您将必须自己设计存储格式并序列化状态。您也许能够使用Java序列化,但需要付出一些努力。
How do you think Excel does this? It stores the type and value of each cell, along with metadata describing the worksheet in its own proprietary binary format in a file. If you have a custom application with complex internal state, you will have to design a storage format and serialize the state yourself. You may be able to use Java Serialization, but not without some effort.
Swing 应用程序框架提供了一种在应用程序运行时保存会话状态的方法退出并在重新启动时恢复状态。会话状态是应用程序的图形窗口配置。此状态包括窗口大小、内部框架位置、选定的选项卡、列宽和其他图形属性。
The Swing Application Framework provides a way to save session state when your application exits and restore the state when you restart. Session state is the graphical window configuration of your application. This state includes window size, internal frame locations, selected tabs, column widths, and other graphical properties.