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

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

问题描述

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

我可以处理对象序列化/反序列化到/从一个文件,但不知道它应该放置或是否有一些登记系统,我可以节省几个用户的设置。

这些设置是依赖于硬件,所以我想将其保存在客户端。

全部权限给予小程序。


解决方案

  

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


有关受信任的小程序,有很多的选择。


  

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


放入的user.home 的子目录中的信息。


  • 的user.home 将是一个地方,是可写的。

  • 为了避免与其他应用程序的设置文件碰撞
  • 的子目录(例如,基于Applet类的包名)。


  

..或者,如果有一些登记系统,我可以节省几个用户的设置。


我听说的 preferences 类可用于该..


  

此数据持久存储在依赖于实现的后备存储。典型实现包括纯文本文件,特定于操作系统的注册表,目录服务器和SQL数据库。这个类的用户不必关心后备存储的详细信息。


听起来很整洁,不是吗?唯一的麻烦是,我从来没有能够作出一个例子,其中的值运行之间依然存在!


对象序列化配备了一个巨大的警告,它可能在任何时候中断。


我会去为自己规范的文件位置(例如,在的user.home ),要么使用的 属性 文件(简单的名称/ XMLEn codeR /的 XMLDe codeR (用于更复杂的Java bean)。

对于前者,看到这个<一个href=\"http://stackoverflow.com/questions/7777640/what-is-the-best-practice-for-setting-jframe-locations-in-java/7778332#7778332\">little例如的。后者是在很短的例子的JavaDoc顶部的描述。


当然,如果该applet部署在一个插件2架构JRE,并已获得了JNLP API,它可以使用 PersistenceService 。这里是一个演示。的 PersistenceService

即使是一个沙箱的小程序可以使用 PersistenceService - 它类似于饼干的概念,它适用于少量数据

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.

解决方案

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

Put the information in a sub-directory of 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.

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

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.


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).

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

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天全站免登陆