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:com.employee.scheduler.common.persistence.AbstractTxtSolutionExporter.java

public void writeSolution(Solution solution, File outputFile) {
    BufferedWriter bufferedWriter = null;
    try {/*from ww w . j a  va 2  s  . co  m*/
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
        TxtOutputBuilder txtOutputBuilder = createTxtOutputBuilder();
        txtOutputBuilder.setBufferedWriter(bufferedWriter);
        txtOutputBuilder.setSolution(solution);
        txtOutputBuilder.writeSolution();
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not write the file (" + outputFile.getName() + ").", e);
    } finally {
        IOUtils.closeQuietly(bufferedWriter);
    }
    logger.info("Exported: {}", outputFile);
}

From source file:com.linkedin.pinot.tools.admin.command.AbstractBaseAdminCommand.java

public static String sendPostRequest(String urlString, String payload)
        throws UnsupportedEncodingException, IOException, JSONException {
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();

    conn.setDoOutput(true);//  w w w.  jav a 2s. c  om
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

    writer.write(payload, 0, payload.length());
    writer.flush();

    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    final StringBuilder sb = new StringBuilder();
    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}

From source file:com.github.douglasjunior.japriori.datatarget.CSVDataTarget.java

private void open() throws IOException {
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
    csvPrinter = csvFormat.print(printWriter);
    csvPrinter.printRecord(HEADERS);//from ww w.  j  a  v  a 2 s . c om
}

From source file:at.tugraz.ist.akm.webservice.requestprocessor.HttpResponseDataAppender.java

public void appendHttpResponseData(HttpResponse httpResponse, final String contentType, final String data) {

    httpResponse.setEntity(new EntityTemplate(new ContentProducer() {
        @Override/*from ww  w.j  ava2 s  . c  om*/
        public void writeTo(OutputStream outstream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outstream, mDefaultEncoding);
            writer.write(data);
            writer.flush();
            writer.close();
        }
    }));
    httpResponse.setHeader(WebServerConstants.HTTP.KEY_CONTENT_TYPE, contentType);
}

From source file:fitnesse.slim.SlimClient.java

public void connect() throws IOException {
    try {/*from  w  ww .  j  a v a2 s.c o  m*/
        for (int tries = 0; !tryConnect(); tries++) {
            if (tries > 100)
                throw new SlimError("Could not start Slim.");
            Thread.sleep(50);
        }
    } catch (InterruptedException e) {
        throw new SlimError("Interrupted while attempting to connect", e);
    }
    reader = new StreamReader(client.getInputStream());
    try {
        writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new ImpossibleException("UTF-8 is a supported encoding", e);
    }
    slimServerVersionMessage = reader.readLine();
    slimServerVersion = isConnected() ? Double.parseDouble(slimServerVersionMessage.replace("Slim -- V", ""))
            : -1;
}

From source file:net.acesinc.data.json.generator.log.JsonFileLogger.java

private void flushToJsonGzip(String event) throws IOException {
    try (FileOutputStream output = new FileOutputStream(outputDirectory)) {
        try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8")) {
            writer.append(event).append("\n");
        }/* w w w.ja va 2  s.  c om*/
    }
}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {//w w w .  j a  v  a2s .co m
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
        jaxbMarshaller.marshal(content, osw);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return baos != null ? baos.toString() : null;
}

From source file:Main.java

public static void prettyPrintXml(Document doc, OutputStream out, int indent) {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // set indent spaces on factory
    //        tfactory.setAttribute("indent-number", new Integer(indent));

    try {//from  w w  w .j av a 2  s.c  om
        Transformer serializer = tfactory.newTransformer();

        // turn on indenting on transformer (serializer)
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
                //                  String.valueOf(indent));
                "2");

        serializer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "utf-8")));
    } catch (Exception e) {

        // this is fatal, just dump stack and throw runtime exception
        e.printStackTrace();

        throw new RuntimeException(e);
    }
}

From source file:org.dkpro.lab.reporting.ChartUtil.java

/**
 * Exports a JFreeChart to a SVG file./*from w  w  w . ja  v a  2s .  c  om*/
 *
 * @param chart JFreeChart to export
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 * @see <a href="http://dolf.trieschnigg.nl/jfreechart/">Saving JFreeChart as SVG vector images
 *      using Batik</a>
 */
public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight)
        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, new Rectangle(aWidth, aHeight));

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

From source file:com.amalto.core.storage.FullTextResultsWriter.java

@Override
public void write(DataRecord record, OutputStream output) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8"); //$NON-NLS-1$
    write(record, writer);/*w  w  w .  ja v  a  2  s  . c  o  m*/
}