StringWriter写入文件夹

时间:2013-12-04 20:53:11

标签: java directory stringwriter

下午好。我想使用StringWriter将新文件写入网络文件夹。任何人都可以使用下面的代码给我一些例子来说明如何做到这一点?这是我第一次使用StringWriter类。

    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());

    }
}

1 个答案:

答案 0 :(得分:0)

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());

}