Java:将对象保存在文本文件中? 有现成的解决方案吗?

发布于 2024-07-07 10:08:54 字数 93 浏览 10 评论 0原文

我想保存我在程序中生成的对象。 重新启动后,应用程序应自动加载数组中的所有对象。 我想将它们写入文件并在重新启动后解析它们。 还有其他比手工更聪明的可能性吗? 谢谢

I want to save the objects I generated in a program. After restart the App should load automaticly all Objects in an Array. I want to write them in a file and parse them after restart. Are the other smarter possibilities than do it by hand?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

黯然#的苍凉 2024-07-14 10:08:54

是的,您正在寻找的概念称为序列化。 Sun 此处有一个很好的教程。

这个想法是你想要持久化的类必须实现 Serialized 接口。 之后,您使用 java.io.ObjectOutputStream.writeObject() 将对象写入文件,并使用 java.io.ObjectInputStream.readObject() 将其读回。

您无法序列化所有内容,因为有些内容序列化没有意义,但您可以解决它们。 这是对此的引用:

Java的基本机制
序列化使用起来很简单,但是
还有一些事情需要了解。 作为
之前提到过,只有标记的对象
可序列化可以持久化。 这
java.lang.Object 类没有
实现该接口。 所以,
并不是所有Java中的对象都可以
自动持续。 好消息
是他们中的大多数——比如 AWT 和
Swing GUI 组件、字符串和
数组——可序列化。

另一方面,某些
系统级类,例如Thread,
OutputStream 及其子类,以及
套接字不可序列化。 的确,
如果他们这样做就没有意义了
是。 例如,线程运行在
我的 JVM 将使用我系统的
记忆。 坚持并努力
在你的 JVM 中运行它是没有意义的
根本不。 另一个重要的一点是关于
java.lang.Object 没有实现
可序列化接口是任何
您创建的仅扩展的类
对象(并且没有其他可序列化的
类)不可序列化,除非
你自己实现接口
(如前面的示例所示)。

这种情况存在一个问题:
如果我们有一个包含以下内容的类怎么办
线程的一个实例? 在这种情况下,
我们可以持久化那个对象吗
类型? 答案是肯定的,只要我们
告诉序列化机制我们的
通过标记我们班的意图
线程对象作为瞬态。

Yes, the concept you are looking for is called serialization. There's a fine tutorial at Sun here.

The idea is that classes you want to persist have to implement the Serializable interface. After that you use java.io.ObjectOutputStream.writeObject() to write the object to a file and java.io.ObjectInputStream.readObject() to read it back.

You can't serialize everything, as there are things that don't make sense to serialize, but you can work around them. Here's a quote about that:

The basic mechanism of Java
serialization is simple to use, but
there are some more things to know. As
mentioned before, only objects marked
Serializable can be persisted. The
java.lang.Object class does not
implement that interface. Therefore,
not all the objects in Java can be
persisted automatically. The good news
is that most of them -- like AWT and
Swing GUI components, strings, and
arrays -- are serializable.

On the other hand, certain
system-level classes such as Thread,
OutputStream and its subclasses, and
Socket are not serializable. Indeed,
it would not make any sense if they
were. For example, thread running in
my JVM would be using my system's
memory. Persisting it and trying to
run it in your JVM would make no sense
at all. Another important point about
java.lang.Object not implementing the
Serializable interface is that any
class you create that extends only
Object (and no other serializable
classes) is not serializable unless
you implement the interface yourself
(as done with the previous example).

That situation presents a problem:
what if we have a class that contains
an instance of Thread? In that case,
can we ever persist objects of that
type? The answer is yes, as long as we
tell the serialization mechanism our
intentions by marking our class's
Thread object as transient.

鸵鸟症 2024-07-14 10:08:54

您可以使用 Berkeley DB PersistentMap 类在 Map 实现(缓存)将它们保存到文件中。 它使用起来非常简单,意味着您不必担心将哪些内容保存在哪里。

关于序列化需要注意的三件事:

  1. 您将如何应对架构更改(即更改您的类所包含的数据 - 例如添加新字段)
  2. 如果您的文件损坏,会发生什么? 程序存储所需的可靠性将影响您如何保存对象隐式包含的数据的决定。 请记住,您始终可以使用关系数据库,例如 MySQL,然后将此数据转换为您的对象。
  3. 您是否需要能够在程序外部查看或查询数据? 如果您想回答一些简单的问题,例如“有多少对象具有属性 X?”,该怎么办? 如果您使用过(关系)数据库,则很容易做到; 如果您已将内容序列化到文件中,就不那么容易了!

You can use the Berkeley DB PersistentMap class to save your (Serializable) objects in a Map implementation (a cache) which persists them to a file. It's pretty simple to use and means you don't have to worry about what to save where.

Three things to note about serialization:

  1. How are you going to cope with schema changes (i.e. changing what data your classes consist of - adding a new field for example)
  2. What happens if your file(s) become corrupted? Depending on how reliable your program's storage needs to be will affect your decision of how you save the data which your objects implicitly contain. Remember, you can always use a relational database such as MySQL and then convert this data into your objects
  3. Do you need to be able to view or query the data outside of your program? What if you want to answer some simple question like "How many object's have property X?" Easy to do if you've used a (relational) database; not so easy if you've serialized stuff to files!
染柒℉ 2024-07-14 10:08:54

您可能想查看《Effective Java》,了解有关序列化的一些问题。 这些通常是高级的,但如果此功能是您应用程序的核心部分,您将需要提前了解它们。

例子包括安全问题、继承,以及最重要的是公开“冻结”API 并实现它的潜力(例如跨软件版本)。 再说一次,这些都是先进的,不应该阻止您本身。

You may want to check out Effective Java for some gotchas with respect to serialization. These are generally advanced but if this feature is a core part of your app, you'll want to know about them in advance.

Examples include security concerns, inheritance, and most importantly the potential to publicly 'freeze' an API with realizing it (e.g. across versions of software). Again, these are all advanced and should not deter you per se.

山有枢 2024-07-14 10:08:54

java.io.Object[Input/Output]Stream 是您需要查看的两个类。
任何您希望保存到文件的类都需要实现 java.io.Serializing 接口。

java.io.Object[Input/Output]Stream are the two classes you need to look at.
Any class you wish to persist to file needs to implement the java.io.Serializable interface.

魂牵梦绕锁你心扉 2024-07-14 10:08:54

您可能还想考虑使用 XML 编码,在我看来,它比序列化具有更高的耐用性。 使用 java.beans.XMLEncoder/XMLDecoder 类。

You might also want to consider using XML encoding, which seems to me to have more durability than serialization. Use java.beans.XMLEncoder/XMLDecoder classes.

安静被遗忘 2024-07-14 10:08:54

我没有使用过它,但 Google Code 最近发布了 protobuf

I have not used it but Google Code released protobuf recently.

坚持沉默 2024-07-14 10:08:54

我同意 mjlee 的观点,Google 的协议缓冲区是一种更好的方法与原始序列化方式相比,确实存储对象。 看一看,您一定会喜欢的。

I concur with mjlee that Protocol Buffers from Google is a better way to do store objects than the original serialization way. Take a look and you will love it.

淡忘如思 2024-07-14 10:08:54

我想知道为什么没有人提到 JSON ,例如 Jackson JSON 序列化器

I wonder why no one mentioned JSON and, for instance, Jackson JSON serializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文