Java applet 中的对象持久性
保存 Java 小程序状态的好方法是什么?
我可以处理文件的对象序列化/反序列化,但不知道它应该放在哪里,或者是否有一些“注册表”,我可以在其中保存几个用户的设置。
这些设置取决于硬件,因此我想将其保存在客户端上。
已向小程序授予完全权限。
What is a good approach to save state of a Java applet?
I can deal with object serialization/deserialization to/from a file but don't know where it should be placed or if there's some 'registry' where I can just save a couple of user's settings.
Those settings are hardware dependent so I want to save it on client.
Full permission is given to the applet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于一个值得信赖的小程序,有很多选择。
将信息放在
user.home
的子目录中。user.home
将是一个可写的地方。我听说
Preferences
类可以用于此目的..听起来很不错,不是吗?唯一的麻烦是我从来无法制作一个例子,其中值在运行之间保持不变!
对象序列化伴随着一个巨大的警告,即它可能随时中断。
我会选择您自己规范的文件位置(例如在
user.home
中)并使用Properties
文件(对于简单的名称/值对)XMLEncoder
/< a href="http://download.oracle.com/javase/7/docs/api/java/beans/XMLDecoder.html" rel="nofollow noreferrer">XMLDecoder
(对于更复杂的 Java 豆子)。对于前者,请参阅此 小例子。后者在 JavaDocs 顶部的一个简短示例中进行了描述。
当然,如果这个小程序部署在 Plug-In 2 架构的 JRE 中并且可以访问 JNLP API,那么它可以使用
PersistenceService
。这是一个 演示。PersistenceService
的。即使是沙盒小程序也可以使用
PersistenceService
- 它类似于 Cookie 的概念,因为它适用于少量数据。For a trusted applet, there are many options.
Put the information in a sub-directory of
user.home
.user.home
will be a place that is writable.I've heard that the
Preferences
class can be used for that ..Sounds neat, doesn't it? The only trouble is that I've never been able to make an example where the values persist between runs!
Object serialization comes with a huge warning that it might break at any time.
I'd go for a file location of your own specification (e.g. in
user.home
) and either use aProperties
file (for simple name/value pairs) ofXMLEncoder
/XMLDecoder
(for more complex Java beans).For the former, see this little example. The latter is described in a short example at the top of the JavaDocs.
Of course, if this applet is deployed in a Plug-In 2 architecture JRE and has access to the JNLP API, it can use the
PersistenceService
. Here is a demo. of thePersistenceService
.Even a sand-boxed applet can use the
PersistenceService
- it is similar to the concept of Cookies in that it is intended for small amounts of data.当需要在浏览器会话之间保留数据时,Applet Persistence API 似乎是一个好方法:http://docs.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/persistence.html
The Applet Persistence API seems to be a good approach when data needs to be persisted between browser sessions: http://docs.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/persistence.html