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 print(Node node, OutputStream out)
        throws UnsupportedEncodingException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    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(node), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

public static void saveUtfFileWithBOM(File file, String content) throws IOException {
    BufferedWriter bw = null;/*from ww  w . ja  v  a2 s .com*/
    OutputStreamWriter osw = null;

    FileOutputStream fos = new FileOutputStream(file);
    try {
        // write UTF8 BOM mark if file is empty
        if (file.length() < 1) {
            final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
            fos.write(bom);
        }

        osw = new OutputStreamWriter(fos, "UTF-8");
        bw = new BufferedWriter(osw);
        if (content != null) {
            bw.write(content);
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        try {
            bw.close();
            fos.close();
        } catch (Exception ex) {
        }
    }
}

From source file:mtsar.api.csv.WorkerRankingCSV.java

public static void write(Collection<WorkerRanking> rankings, OutputStream output) throws IOException {
    try (final Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
        final Iterable<String[]> iterable = () -> rankings.stream().sorted(ORDER)
                .map(aggregation -> new String[] { Integer.toString(aggregation.getWorker().getId()), // worker_id
                        Double.toString(aggregation.getReputation()) // reputation
                }).iterator();/*  w ww .j a v  a  2 s. com*/

        FORMAT.withHeader(HEADER).print(writer).printRecords(iterable);
    }
}

From source file:mtsar.api.csv.AnswerAggregationCSV.java

public static void write(Collection<AnswerAggregation> aggregations, OutputStream output) throws IOException {
    try (final Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
        final Iterable<String[]> iterable = () -> aggregations.stream().sorted(ORDER)
                .map(aggregation -> new String[] { Integer.toString(aggregation.getTask().getId()), // task_id
                        String.join("|", aggregation.getAnswers()) // answers
                }).iterator();/*from w  ww  .  j  a v  a2s  .c o  m*/

        FORMAT.withHeader(HEADER).print(writer).printRecords(iterable);
    }
}

From source file:Main.java

/**
 * Writes the given {@link Document} to an {@link OutputStream} using
 * UTF-8 encoding.//from  w ww . j a  v a2  s . com
 *
 * @param doc The {@link Document} to write.
 * @param out The {@link OutputStream} to write to.
 * @see #encodeDocument(Document,boolean)
 * @see #writeDocumentTo(Document,File)
 */
public static void writeDocumentTo(Document doc, OutputStream out) throws IOException {
    final DOMSource source = new DOMSource(doc);
    final Writer writer = new OutputStreamWriter(out, "UTF-8");
    final StreamResult result = new StreamResult(writer);
    final Transformer xform = createTransformer();
    try {
        xform.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Couldn't write XML: " + e.getMessage());
    }
}

From source file:org.jfreechart.SVGExporter.java

public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, bounds);//  w  w  w  . j a  va 2s  .  com

    // Write svg file
    OutputStream outputStream = new FileOutputStream(svgFile);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    outputStream.flush();
    outputStream.close();
}

From source file:Main.java

public static void writeToFile(Document doc, String file) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(doc);
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
}

From source file:StreamConverter.java

static void writeOutput(String str) {

    try {/*  w w w .jav  a  2  s  .  c om*/
        FileOutputStream fos = new FileOutputStream("test.txt");
        Writer out = new OutputStreamWriter(fos, "UTF8");
        out.write(str);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.digitisation.idiomaident.utils.ExtractTestSamples.java

private static void extractSamples(int numSamples, File inFolder, File outFile) {
    FileOutputStream fileOS = null;
    OutputStreamWriter outSWriter = null;

    try {/* www  . ja va  2s .co m*/
        fileOS = new FileOutputStream(outFile);
        outSWriter = new OutputStreamWriter(fileOS, Charset.forName("UTF8"));

        int num = 0;
        while (num < numSamples) {
            String sample = nextSample(inFolder);

            if (sample == null)
                continue;

            outSWriter.write(sample);
            num++;
            System.out.println("Generating " + num + " of " + numSamples + " samples");
        }

        outSWriter.flush();

    } catch (IOException ex) {
        System.out.println(ex.toString());
    } finally {
        try {
            if (outSWriter != null) {
                outSWriter.close();
            }
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

}

From source file:Main.java

/**
 * Save {@link String} to {@link File} witht the specified encoding
 *
 * @param string {@link String}/*  w  w w .j a  v  a2s. c  o  m*/
 * @param path   Path of the file
 * @param string Encoding
 * @throws IOException
 */
public static void saveStringToFile(String string, File path, String encoding) throws IOException {
    if (path.exists()) {
        path.delete();
    }

    if ((path.getParentFile().mkdirs() || path.getParentFile().exists())
            && (path.exists() || path.createNewFile())) {
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding));
        writer.write(string);
        writer.close();
    }
}