Java 小程序中的对象持久性 [英] Object persistence in Java applets

查看:30
本文介绍了Java 小程序中的对象持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保存 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.

授予小程序完全权限.

推荐答案

保存 Java 小程序状态的好方法是什么?

What is a good approach to save state of a Java applet?

对于受信任的小程序,有很多选择.

For a trusted applet, there are many options.

我可以处理到/从文件的对象序列化/反序列化,但不知道它应该放在哪里..

I can deal with object serialization/deserialization to/from a file but don't know where it should be placed..

将信息放在user.home的子目录中.

Put the information in a sub-directory of user.home.

  • user.home 将是一个可写的地方.
  • 一个子目录(例如基于小程序类的包名),以避免与其他应用的设置文件发生冲突.
  • user.home will be a place that is writable.
  • A sub-directory (e.g. based on the package name of the applet class) in order to avoid colliding with the settings files of other apps.

...或者如果有一些注册表",我可以在其中保存几个用户的设置.

..or if there's some 'registry' where I can just save a couple of user's settings.

我听说 Preferences 类可用于..

I've heard that the Preferences class can be used for that ..

此数据永久存储在依赖于实现的后备存储中.典型的实现包括平面文件、特定于操作系统的注册表、目录服务器和 SQL 数据库.这个类的用户不需要关心后备存储的细节.

This data is stored persistently in an implementation-dependent backing store. Typical implementations include flat files, OS-specific registries, directory servers and SQL databases. The user of this class needn't be concerned with details of the backing store.

听起来不错,不是吗?唯一的问题是我从来没有能举出一个例子,在运行之间值保持不变!

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.

我会查找您自己规范的文件位置(例如在 user.home 中)并使用 Properties 文件(用于简单的名称/值对)XMLEncoder/XMLDecoder(用于更复杂的 Java bean).

I'd go for a file location of your own specification (e.g. in user.home) and either use a Properties file (for simple name/value pairs) of XMLEncoder/XMLDecoder (for more complex Java beans).

对于前者,请参阅此 小例子.后者在 JavaDocs 顶部的一个简短示例中进行了描述.

For the former, see this little example. The latter is described in a short example at the top of the JavaDocs.

当然,如果这个小程序部署在 Plug-In 2 架构的 JRE 中并且可以访问 JNLP API,它可以使用 PersistenceService.这是一个 演示.PersistenceService.

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 the PersistenceService.

即使是沙盒小程序也可以使用 PersistenceService - 它类似于 Cookie 的概念,因为它适用于少量数据.

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.

这篇关于Java 小程序中的对象持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆