我正在为我的高级 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:
- 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.
- 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/