java 追加到文件
我用谷歌搜索了一段时间,但似乎找不到它,它应该很容易。 我想将 CR 附加到我使用 Transformer 创建的 XML 文件的末尾。 有没有办法做到这一点>
我尝试了以下操作,但这导致了一个空白文件?
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "file:///ReportWiz.dtd");
xformer.transform(source, result);
OutputStream writer = new FileOutputStream(file);
Byte b = '\n';
writer.write(b);
writer.close();
I googled for this for a while but can't seem to find it and it should be easy. I want to append a CR to then end of an XML file that I am creating with a Transformer. Is there a way to do this>
I tried the following but this resulted in a blank file?
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "file:///ReportWiz.dtd");
xformer.transform(source, result);
OutputStream writer = new FileOutputStream(file);
Byte b = '\n';
writer.write(b);
writer.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显而易见的答案是将 StreamResult 与 Writer 一起使用,并在关闭之前将您的角色附加到 writer 中。
您似乎正在尝试重新打开文件以附加字符。
FileOutputStream
构造函数的两个参数形式包含一个附加标志。The obvious answer is to use StreamResult with a Writer, and append your character to the writer before closing.
You appear to be trying to reopen the file to append the character. The two argument forms of the
FileOutputStream
constructors include an append flag.我不知道这个 Transformer 类。 但我发现您的 writer/file 变量和 xformer/source/result 变量之间没有任何联系...看起来您只写了一个换行符。
除非你省略了一些重要的部分。
I didn't know this Transformer class. But I see no connection between your writer/file variables and your xformer/source/result variables... Looks like you write only a newline.
Unless you omitted some essential part.
请注意,请确保使用用于 xml 序列化的相同字符编码编写换行符,否则文件将被损坏。
one note, make sure you write your newline character using the same character encoding you used for the xml serializaton, or your file will be broken.
简单...只需添加 附加选项:
Simple... just add the append option:
这里有几个选项。
我假设您的结果是您使用指定目标文件路径的
String
创建的StreamResult
。 您可能会考虑自己打开一个FileOutputStream
,并用它构建您的StreamResult
。 然后,当变压器完成后,附加行终止符、刷新并关闭流。 如果没有其他限制,我会使用这种方法。如果您想重新打开文件,如问题所示,您需要使用一个
FileOutputStream
构造函数,该构造函数采用可选的追加
参数。 将其设置为true
以避免破坏刚刚完成的转换结果。您还可以探索设置
indent
转换中的输出属性,或直接在模板中包含必要的行终止符。There are a couple of options here.
I assume that your result is a
StreamResult
that you are creating with aString
specifying the destination file path. You might consider opening aFileOutputStream
yourself, and constructing yourStreamResult
with that. Then, when the transformer is done, append the line terminator, flush, and close the stream. In the absence of constraints otherwise, I would use this approach.If you want to re-open the file, as shown in the question, you'll need to use a
FileOutputStream
constructor that takes the optionalappend
argument. Set this totrue
to avoid clobbering the result of the transform that was just completed.You might also explore setting the
indent
output property in your transform, or including the necessary line terminator directly in your template.