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

/**
 * Saves a WC3 DOM by writing it to an XML document.
 *
 * @param doc The WC3 DOM document object.
 * @param docPath The full path to the XML document.
 * @param encoding Encoding scheme to use for the XML document, e.g.,
 * "UTF-8."/*from   www  . j  av  a2  s  .com*/
 * @throws TransformerConfigurationException
 * @throws FileNotFoundException
 * @throws UnsupportedEncodingException
 * @throws TransformerException
 * @throws IOException
 */
public static void saveDocument(final Document doc, String encoding, String docPath)
        throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException,
        TransformerException, IOException {
    TransformerFactory xf = TransformerFactory.newInstance();
    xf.setAttribute("indent-number", 1); //NON-NLS
    Transformer xformer = xf.newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
    File file = new File(docPath);
    try (FileOutputStream stream = new FileOutputStream(file)) {
        Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
        xformer.transform(new DOMSource(doc), out);
        stream.flush();
    }
}

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

public static void write(Collection<Worker> workers, OutputStream output) throws IOException {
    try (final Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
        final Iterable<String[]> iterable = () -> workers.stream().sorted(ORDER)
                .map(worker -> new String[] { Integer.toString(worker.getId()), // id
                        worker.getStage(), // stage
                        Long.toString(worker.getDateTime().toInstant().getEpochSecond()), // datetime
                        String.join("|", worker.getTags()), // tags
                }).iterator();/*  w w w  .j  a v  a2  s .  co  m*/

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

From source file:net.ontopia.xml.CanonicalPrinter.java

/**
 * Creates a CanonicalPrinter that writes to the given OutputStream.
 * The encoding used is always utf-8./*from www. j a v a  2 s  . c o  m*/
 */
public CanonicalPrinter(OutputStream stream, boolean closeWRiter) {
    try {
        this.writer = new PrintWriter(new OutputStreamWriter(stream, "utf-8"));
    } catch (UnsupportedEncodingException use) {
        throw new OntopiaRuntimeException(use);
    }
    this.closeWRiter = closeWRiter;
}

From source file:com.serphacker.serposcope.db.base.ExportDB.java

public boolean export(String path) throws Exception {
    OutputStream os = new FileOutputStream(path, false);
    if (path.endsWith(".gz")) {
        os = new GZIPOutputStream(os);
    }//from ww  w.j  a  v a  2 s  .  c o m
    try (OutputStreamWriter osw = new OutputStreamWriter(os, Charset.forName("UTF-8"));
            PrintWriter writer = new PrintWriter(osw);) {
        return export(writer);
    }
}

From source file:com.wesley.urban_cuts.services.barber_services.Write_to_file.java

public void write_to_file(String data1, String data2, String data3) {
    Date d = new Date();
    Writer writer = null;/*  w  w  w . j  av a 2  s .co m*/
    try {
        writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream("Urban Cuts Log" + ".txt"), "utf-8"));

        writer.append("");
        writer.append(data1 + "   " + data2 + "   " + data3 + "   " + d);

    } catch (IOException ex) {
        System.out.println("couldn't write to file");
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
            /*ignore*/}
    }
}

From source file:com.creactiviti.piper.plugin.ffmpeg.Vstitch.java

@Override
public Object handle(Task aTask) throws Exception {
    List<String> chunks = aTask.getList("chunks", String.class);
    File tempFile = File.createTempFile("_chunks", ".txt");
    try {/*from  ww  w . j  a v  a  2s.c  o m*/
        try (Writer writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(tempFile, true), "UTF-8"))) {
            for (String chunk : chunks) {
                writer.append(String.format("file '%s'", chunk)).append("\n");
            }
        }
        SimpleTaskExecution ffmpegTask = SimpleTaskExecution.create();
        List<String> options = Arrays.asList("-y", "-f", "concat", "-safe", "0", "-i",
                tempFile.getAbsolutePath(), "-c", "copy", aTask.getRequiredString("output"));
        ffmpegTask.set("options", options);
        ffmpeg.handle(ffmpegTask);
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
    return null;
}

From source file:it.tizianofagni.sparkboost.DataUtils.java

/**
 * Write a text file on Hadoop file system by using standard Hadoop API.
 *
 * @param outputPath The file to be written.
 * @param content    The content to put in the file.
 *//*from   www .ja v a 2s .c o  m*/
public static void saveHadoopTextFile(String outputPath, String content) {
    try {
        Configuration configuration = new Configuration();
        Path file = new Path(outputPath);
        Path parentFile = file.getParent();
        FileSystem hdfs = FileSystem.get(file.toUri(), configuration);
        if (parentFile != null)
            hdfs.mkdirs(parentFile);
        OutputStream os = hdfs.create(file, true);
        BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        br.write(content);
        br.close();
        hdfs.close();
    } catch (Exception e) {
        throw new RuntimeException("Writing Hadoop text file", e);
    }
}

From source file:com.redhat.akashche.wixgen.jaxb.SampleInstallerTest.java

@Test
public void test() throws Exception {
    JAXBContext jaxb = JAXBContext.newInstance(SampleInstallerTest.class.getPackage().getName());
    InputStream is = null;/*from  ww w  .j a  v a  2 s. com*/
    Writer writer = null;
    try {
        is = SampleInstallerTest.class.getResourceAsStream("SampleInstaller.wxs");
        Reader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
        Wix wix = (Wix) jaxb.createUnmarshaller().unmarshal(reader);
        wix.getProduct().setName(wix.getProduct().getName() + " (JAXB)");
        //            OutputStream os = new FileOutputStream("SampleInstaller_JAXB.wxs");
        OutputStream os = new NullOutputStream();
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        Marshaller marshaller = jaxb.createMarshaller();
        marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(wix, writer);
    } finally {
        closeQuietly(is);
        closeQuietly(writer);
    }
}

From source file:com.predic8.membrane.core.interceptor.WADLInterceptor.java

@Override
protected void rewrite(Exchange exc) throws Exception, IOException {

    log.debug("Changing endpoint address in WADL");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    Relocator relocator = new Relocator(new OutputStreamWriter(stream, exc.getResponse().getCharset()),
            getLocationProtocol(), getLocationHost(exc), getLocationPort(exc), pathRewriter);

    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "resources"), "base");
    relocator.getRelocatingAttributes().put(new QName(WADL_NS, "include"), "href");

    relocator.relocate(/*from w w w .  j a va  2 s.  c o  m*/
            new InputStreamReader(exc.getResponse().getBodyAsStreamDecoded(), exc.getResponse().getCharset()));

    exc.getResponse().setBodyContent(stream.toByteArray());
}

From source file:edu.usc.goffish.gofs.formats.gml.GMLWriter.java

public GMLWriter(OutputStream output, boolean writeIndentation, int bufferSize) {
    _output = new BufferedWriter(new OutputStreamWriter(output, GMLParser.GML_CHARSET), bufferSize);
    _writeIndentation = writeIndentation;
    _indentation = 0;//from   w  ww  .  j  a  v  a2 s  .  c  o m
}