Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

In this page you can find the example usage for java.io OutputStreamWriter OutputStreamWriter.

Prototype

public OutputStreamWriter(OutputStream out, CharsetEncoder enc) 

Source Link

Document

Creates an OutputStreamWriter that uses the given charset encoder.

Usage

From source file:Main.java

public static OutputStream streamDocument(Document doc) throws IOException, TransformerException {
    OutputStream out = null;// ww  w  .  j  a  v a 2s.co  m
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "no");
    transformer.setOutputProperty("method", "xml");
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));

    return out;
}

From source file:Main.java

/**
 * @param dom//from w w w  .  ja  v  a2  s . c o  m
 * @param outFile
 */
public static void writeDomToFile(Document dom, File outFile) {
    try {
        Transformer transformer = transFactory.newTransformer();
        DOMSource source = new DOMSource(dom);
        StreamResult result;

        result = new StreamResult(
                new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding")));
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException(
                "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e);
    }
}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "no");
    transformer.setOutputProperty("method", "xml");
    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty("encoding", "UTF-8");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

public static String Document2String(Document doc) throws IOException, TransformerException {

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(baos, "UTF-8")));
    return baos.toString("UTF-8");
}

From source file:Main.java

public static void printDocument(Document doc) {
    try {/* w w  w.j a v a 2 s.  co m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void printDocument(final Document doc) {
    try {//  w  w  w .  j  av  a  2s  . c  om
        final TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
}

From source file:io.mapzone.controller.vm.http.ProvisionErrorResponse.java

public static void send(HttpServletResponse response, int status, String msg) {
    try {/*from  w ww .j  a va  2 s  .  c o m*/
        response.setContentType("text/html");
        OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), Charset.forName("UTF-8"));
        out.write("<html>\n");
        out.write("<h1>" + msg + "</h1>\n");
        out.write("</html>");
        out.flush();
        response.setStatus(status);
    } catch (IOException e) {
        log.warn("", e);
    }
}

From source file:Main.java

public static void saveToFile(File folder, String name, String data) {

    File file = new File(folder, name);

    PrintWriter pw = null;/*from   w  w w  .j ava  2  s  . c o  m*/
    try {
        Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        pw = new PrintWriter(w);
        pw.write(data);

    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}

From source file:Main.java

/**
 * /*from  w  ww  .  j  av a  2  s.co m*/
 * @param os
 * @param request
 * @param charsetName
 * @throws IOException
 */
public static final void writeString(OutputStream os, String request, String charsetName) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(os, charsetName);
    BufferedWriter bw = new BufferedWriter(writer);
    bw.write(request);
    bw.write("\r\n");
    bw.flush();
}