gpt4 book ai didi

java - 将文档写入内部存储

转载 作者:行者123 更新时间:2023-12-01 13:20:40 25 4
gpt4 key购买 nike

我已经使用 Java DOM 编辑 XML 模板,现在想要将生成的 Document 存储在 Android 私有(private)内部存储中,按照 official API guides .

到目前为止我已经:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(ThisApplication.resources().openRawResource(R.raw.default_store));

// Populate document here.

//Convert document to byte[]
Source source = new DOMSource(doc);
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult result = new StreamResult(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result); /// transformer is null!!!!!!!

// Store byte[] in internal storage
FileOutputStream fos = openFileOutput("data_store", Context.MODE_PRIVATE);
fos.write(out.toByteArray());
fos.close();

目前,我尝试在 transformer 上调用 tranform() 时遇到空指针异常。 TransformerFactory API says newTransformer(); 永远不会返回 null,但显然它也是 platform dependent就我而言,返回 null。

所以,问题是我还能怎样;

A)Document 对象转换为 byte[]

B)找到另一种将文档保存到内部存储的方法吗?

编辑:Android bug report filed .

最佳答案

由于 JDOM 文档是可序列化的,因此您可以只写 Serializable object

我会这样做:

ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(document);
out.close();

关于java - 将文档写入内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com