【发布时间】:2014-04-19 14:45:30
【问题描述】:
我编写了一个简单的程序来将书籍、杂志等对象序列化和反序列化到一个文件中,并且我使用了 Java 实用程序库的 ObjectInputStream/ObjectOutputStream 类。
一切正常,但我想在每个对象后添加一些换行符,以便在打开文件时更具可读性。
在 ObjectInputStream/ObjectOutputStream 类中使用字符串时是否有“\n”的等效项?
PS:我还有一个版本,我使用 XStream 使用 xml 数据进行序列化和反序列化。有没有人有想法,如果也有可能,使用 XStream 进行这样的突破?
OutputStream fos = null;
ObjectOutputStream o = null;
// Now serialize objects into the file
try
{
fos = new FileOutputStream("C:\\Users...");
// Initialize the ObjectOutputStream with XStream when xml_switcher is set to true
if (xml_switcher == true) {
o = xstream.createObjectOutputStream(fos);
}
// Otherwise normal serializing by creating a normal ObjectOutputStream
else {
o = new ObjectOutputStream( fos );
}
ArrayList<Printing> resultList = Access.getAllPrintings();
Iterator<Printing> it = resultList.iterator();
while (it.hasNext()) {
if (xml_switcher == true) {
o.writeObject(it.next());
// So here's my problem: I would like to have something like o.write("\n")
// using XStream
}
else {
o.writeObject(it.next().toString());
// Same here but without XStream
}
}
}
【问题讨论】:
标签: java objectinputstream objectoutputstream