Java:将对象保存在文本文件中? 有现成的解决方案吗?
我想保存我在程序中生成的对象。 重新启动后,应用程序应自动加载数组中的所有对象。 我想将它们写入文件并在重新启动后解析它们。 还有其他比手工更聪明的可能性吗? 谢谢
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,您正在寻找的概念称为序列化。 Sun 此处有一个很好的教程。
这个想法是你想要持久化的类必须实现 Serialized 接口。 之后,您使用 java.io.ObjectOutputStream.writeObject() 将对象写入文件,并使用 java.io.ObjectInputStream.readObject() 将其读回。
您无法序列化所有内容,因为有些内容序列化没有意义,但您可以解决它们。 这是对此的引用:
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:
您可以使用 Berkeley DB
PersistentMap
类在Map
实现(缓存)将它们保存到文件中。 它使用起来非常简单,意味着您不必担心将哪些内容保存在哪里。关于序列化需要注意的三件事:
You can use the Berkeley DB
PersistentMap
class to save your (Serializable
) objects in aMap
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:
您可能想查看《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.
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.
您可能还想考虑使用 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.我没有使用过它,但 Google Code 最近发布了 protobuf。
I have not used it but Google Code released protobuf recently.
我同意 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.
我想知道为什么没有人提到 JSON ,例如 Jackson JSON 序列化器。
I wonder why no one mentioned JSON and, for instance, Jackson JSON serializer.