简体   繁体   English

是否可以以易于阅读的格式将对象发送到文件?

[英]Is it possible to send an object to a file in a format that is human readable?

I am working on a project for my advanced Java class, and the assignment says he wants us to send an object to a file, which is easy enough, but that he also wants the file to be human readable and editable. 我正在为我的高级Java类开发一个项目,该作业说他希望我们将对象发送到文件中,这很容易,但是他还希望文件是人类可读和可编辑的。 I sent him an e-mail 3 days ago and he hasn't responded, so I am kind of stuck between a rock and hard place since the project is due in 3 days. 我三天前给他发送了一封电子邮件,但他没有回复,所以我很困惑,因为该项目将在三天内完成。

So would any of you clever programmers be able to fill me in on the secret that apparently I am left out of. 因此,你们中任何一个聪明的程序员都能以我显然被排除在外的秘密来填补我的秘密。

How do you send an object to a file that reads like English? 如何将对象发送到类似英语的文件中?

I want to have the ability to both read and write a to-do item to a file. 我想同时具有将待办事项读取和写入文件的功能。 I see our application looking like: 我看到我们的应用程序看起来像:

  1. When it first starts, the program asks the user if there is a file containing to-do items. 首次启动时,程序会询问用户是否存在包含待办事项的文件。 If so, the user will name the file, the program will read it in and continue. 如果是这样,用户将命名该文件,程序将读取该文件并继续。
  2. When the user decides to exit, the program will prompt the user - to ask if the to-do items should be saved to a file - if so, the user will name the file and the program will write them out in such a fashion that it can read them in again. 当用户决定退出时,程序将提示用户-询问待办事项是否应保存到文件中-如果这样,用户将命名文件,程序将以这种方式将其写出它可以再次读取它们。

I want these file to be human readable (and editable). 我希望这些文件可读(可编辑)。 No binary data. 没有二进制数据。 No counting. 不计。 My advice to you would be to have a method somewhere that looked like: 我对您的建议是在某处看起来像这样的方法:

 public ToDoItem getToDoItem(FileInputStream fis) { // ... } 

and

 public void writeToDoItem(FileOutputStream fos) { // ... } 

Think of your serialization model. 想想您的序列化模型。 The ObjectOutputStream might write bytes, but is there another way you could represent the object and write it through some other output stream that writes human-readable text? ObjectOutputStream可能会写字节,但是还有另一种方式可以表示对象并通过其他一些可写文本的输出流将其写入?

This is going to depend on the type of object you have. 这将取决于您拥有的对象的类型。 You will have to tailor it to a particular type of data. 您将不得不针对特定类型的数据进行定制。

For example, if you have an Object 例如,如果您有一个对象

String title;
List<Integer> ids;

then you could save it as JSON 然后可以将其另存为JSON

{
   title: 'aaaa',
   ids: [ 1,2,3,4,5 ]
}

which is equivalent, but much more readable than a binary ObjectOutputStream. 这是等效的,但比二进制ObjectOutputStream更具可读性。

Again, this won't work for all kinds of data. 同样,这不适用于所有类型的数据。

There is an XML-based bean serialization, too, which also works with almost all data, but I would not call that human-readable. 也有一个基于XML的bean序列化,它也可以处理几乎所有数据,但是我不会将其称为人类可读的。

You can have a try with JAXB.(Java Architecture for XML Binding) It can send a JAXB styled object to a xml file. 您可以尝试使用JAXB。(用于XML绑定的Java体系结构)它可以将JAXB样式的对象发送到xml文件。 But you should define a XML Schema file at first. 但是您首先应该定义一个XML Schema文件。 For more:http://jaxb.java.net/tutorial/ 有关更多信息:http://jaxb.java.net/tutorial/

Think how you would represent an object on paper in such a way that it could be reconstructed unambiguously. 想一想您将如何以一种可以清晰地重构的方式在纸上表示物体。 You'd probably list the class name, then you'd list each field name and its current value. 您可能会列出类名,然后列出每个字段名及其当前值。 If the field was a primitive, the value would be just the primitive value. 如果该字段是原始值,则该值将只是原始值。 If it was a reference type, you'd represent the object recursively using this procedure. 如果是引用类型,则可以使用此过程递归地表示对象。 If it was an array, you'd list each element value. 如果是数组,则将列出每个元素值。

There are various standard ways of formatting such a representation (XML and JSON to name a couple). 有多种格式化这种表示形式的标准方法(XML和JSON仅举几例)。 The key is to make it a text-only representation so it is human-readable. 关键是使其成为纯文本表示形式,以便于人类阅读。

The human readable that you desire could be XML or JSON . 您想要的人类可读XML可以是XMLJSON My answer How to create object tree from xsd in Java? 我的答案如何在Java中从xsd创建对象树?

might help in giving you pointers to the approach you can follow to achieve what you want. 可能会帮助您找到实现所需目标的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM