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 void DocumentToFile(final Document doc, File file) {
    // FileWriter writer = null;
    OutputStreamWriter outputStreamWriter = null;

    try {/*  ww w  .j  a  v a  2s.c  om*/
        // writer = new FileWriter(file);

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");

    } catch (Exception e) {
        System.err.println(e);

        return;
    }

    //Result l_s = new StreamResult(writer);
    Result l_s = new StreamResult(outputStreamWriter);

    doc.normalize();

    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        outputStreamWriter.close();
        //writer.close();
    } catch (Exception e) {
        System.err.println(e);

        return;
    }
}

From source file:Main.java

/**
 * Comprime uma string utilizando o GZIP
 * //  w ww .j  a v a2 s  . com
 * @param str
 * @param encoding
 * @return
 */
public static String gzipCompressString(String str, String encoding) {
    try {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        Writer writer = new OutputStreamWriter(gzip, encoding);
        writer.write(str);
        writer.close();
        byte[] compressedBytes = out.toByteArray();
        return new String(Base64.encodeBase64(compressedBytes), encoding);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void writeXMLDocToFile(Document outputDoc, String outFileName) {
    try {/*from ww w . j  av  a 2s .  c o  m*/
        //FileWriter writer = new FileWriter( outFileName );
        final String encoding = "UTF-8";
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding);
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(outputDoc);
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Could not create output XML file: " + outFileName);
    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("* Transformer Factory error");
        System.out.println("  " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null)
            x = tce.getException();
        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("* Transformation error");
        System.out.println("  " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null)
            x = te.getException();
        x.printStackTrace();
    }
}

From source file:Main.java

/**
 * @param dom/*from   ww  w .  j  a  va 2s.  co  m*/
 * @param outFile
 */
public static void writeDomToFile(Document dom, File outFile) {
    try {
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer transformer = transFact.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

/**
 * Creates a {@link StreamResult} by wrapping the given outputStream in an
 * {@link OutputStreamWriter} that transforms Windows line endings (\r\n) 
 * into Unix line endings (\n) on Windows for consistency with Roo's templates.  
 * /*  w  w  w .j  a v  a  2  s . c  o m*/
 * @param outputStream
 * @return StreamResult 
 * @throws UnsupportedEncodingException 
 */
private static StreamResult createUnixStreamResultForEntry(OutputStream outputStream)
        throws UnsupportedEncodingException {
    final Writer writer;
    if (System.getProperty("line.separator").equals("\r\n")) {
        writer = new OutputStreamWriter(outputStream, "ISO-8859-1") {
            public void write(char[] cbuf, int off, int len) throws IOException {
                for (int i = off; i < off + len; i++) {
                    if (cbuf[i] != '\r' || (i < cbuf.length - 1 && cbuf[i + 1] != '\n')) {
                        super.write(cbuf[i]);
                    }
                }
            }

            public void write(int c) throws IOException {
                if (c != '\r')
                    super.write(c);
            }

            public void write(String str, int off, int len) throws IOException {
                String orig = str.substring(off, off + len);
                String filtered = orig.replace("\r\n", "\n");
                int lengthDiff = orig.length() - filtered.length();
                if (filtered.endsWith("\r")) {
                    super.write(filtered.substring(0, filtered.length() - 1), 0, len - lengthDiff - 1);
                } else {
                    super.write(filtered, 0, len - lengthDiff);
                }
            }
        };
    } else {
        writer = new OutputStreamWriter(outputStream, "ISO-8859-1");
    }
    return new StreamResult(writer);
}

From source file:Main.java

public static void output(Document doc, OutputStream outputStream) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", 4);

    Transformer transformer = tFactory.newTransformer();
    Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, "yes");
    outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperties(outputProperties);

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new OutputStreamWriter(outputStream, "UTF-8"));
    transformer.transform(source, result);
}

From source file:Main.java

public static void printDocument(Node doc, OutputStream out) {

    try {/*from w w  w  .  jav  a2  s  . com*/
        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")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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("{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) {
    try {//from  w  ww.  j a va2  s . co m
        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", "2");
        transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    } catch (Exception e) {
        System.out.println("Error while printing the document.");
    }
}

From source file:Main.java

public static BufferedWriter createBuffWriter(File file)
        throws FileNotFoundException, UnsupportedEncodingException {
    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
}