[英]Write Object from List to File, using ObjectOutputStream
我正在尝试使用ObjectOutputStream将列表(对象)的内容写入磁盘。
这是相关代码:
//Input Filetype is .xlsx with an embedded File (also .xlsx), Output Filetype should be .xlsx (Type of embedded File)
//This code should save the embedded File to D:\\...
List<HSSFObjectData> extrList = new ArrayList<HSSFObjectData>();
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
extrList = embeddedWorkbook.getAllEmbeddedObjects();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\scan_temp\\emb.xlsx"));
oos.writeObject(extrList);
oos.flush();
oos.close();
这段代码创建了一个名为emb.xlsx的文件,但是内容不是我所期望的。 如果我尝试使用记事本打开,则类似于:
¬í sr java.util.ArrayListxÒ™Ça I sizexp w x
我在这里做错了什么? 谢谢你的帮助。
我在这里做错了什么?
您做错了几件事:
.xlsx
扩展名误用于序列化对象的文件。 该扩展名适用于XML格式的Excel电子表格。 您应该使用.bin
, .data
, .ser
等。 close()
之前多余地调用了flush()
close()
。 如果其他人正在尝试与我相同的操作,请使用以下代码(有效!):
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(InputStream);
FileOutputStream fileOut = new FileOutputStream("/outputfilepath.xls");
embeddedWorkbook.write(fileOut);
fileOut.close();
不要尝试将嵌入的对象放入列表中。 只需使用.write()
。 :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.