java 追加到文件

发布于 2024-07-10 13:41:22 字数 437 浏览 8 评论 0原文

我用谷歌搜索了一段时间,但似乎找不到它,它应该很容易。 我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遥远的她 2024-07-17 13:41:23

显而易见的答案是将 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.

北方。的韩爷 2024-07-17 13:41:23

我不知道这个 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.

豆芽 2024-07-17 13:41:23

请注意,请确保使用用于 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.

别低头,皇冠会掉 2024-07-17 13:41:22

简单...只需添加 附加选项:

    new FileOutputStream(f, true /* append */);

Simple... just add the append option:

    new FileOutputStream(f, true /* append */);
樱娆 2024-07-17 13:41:22

这里有几个选项。

我假设您的结果是您使用指定目标文件路径的 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 a String specifying the destination file path. You might consider opening a FileOutputStream yourself, and constructing your StreamResult 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 optional append argument. Set this to true 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文