首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StringWriter写入文件夹

StringWriter写入文件夹
EN

Stack Overflow用户
提问于 2013-12-04 20:53:10
回答 1查看 424关注 0票数 0

下午好。我想使用StringWriter将新文件写入网络文件夹。有人能用下面的代码给我一些如何做到这一点的例子吗?这是我第一次使用StringWriter类。

代码语言:javascript
复制
    public static final void newOutput(Document xml) throws Exception {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter out = new StringWriter();
        tf.transform(new DOMSource(xml), new StreamResult(out));

        /*
         * need to update to write to folder
         */
        System.out.println(out.toString());

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 21:05:49

代码语言:javascript
复制
public static final void newOutput(Document xml) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(xml);
    // Use StreamResult to write to a file to current directory
    StreamResult out = new StreamResult(new File("test.txt"));
    // to print to console
    // StreamResult out = new StreamResult(System.out);
    tf.transform(source, out);

    /*
     * console output is redirected to SRC folder to check format
     * need to update to write to folder
     */
    System.out.println(out.toString());

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20385689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档