java - 是否可以将对象以人类可读的格式发送到文件?

标签 java file-io serializable

我正在为我的高级 Java 类(class)开发一个项目,作业说他需要我们 将对象发送到文件,这很容易,但他也希望该文件可供人类读取和编辑。我三天前给他发了邮件,他还没有回复,所以 由于该项目还有 3 天就要到期,我陷入了进退两难的境地。

那么你们聪明的程序员是否能够告诉我这个显然我被遗漏的 secret 。

如何将对象发送到读起来像英语的文件?

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) {
 // ...
}

最佳答案

考虑您的序列化模型。 ObjectOutputStream 可能会写入字节,但是是否有另一种方法可以表示对象并通过写入人类可读文本的其他输出流写入它?

关于java - 是否可以将对象以人类可读的格式发送到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9866335/

相关文章:

java - Java从文件加载对象时的变量引用问题

java - 以编程方式签署 JAR

java - 使用 selenium Web 驱动程序获取 NoClassDefFoundError

c - 递归地反转链表数组并不是按顺序反转所有节点

java - 为什么在 JPA 2/Hibernate 中使用共享主键时实体需要可序列化?

java - 字符串 'ACED00057400'代表什么?

java - 将两个字符串与新行合并

java - Ant部署而不是eclipse以简化开发

c# - 快速访问文件中的 key (无需将整个文件加载到内存中)

Python 文件-IO 和 zipfile。尝试循环遍历文件夹中的所有文件,然后使用Python循环遍历各个文件中的文本