Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:OutputStreamDemo.java

static void writeints(String msg, int count, Writer os) throws IOException {
    long currentTime = System.currentTimeMillis();
    for (int i = 0; i < count; i++)
        os.write(i & 255);//ww  w .  j  a v  a  2  s  . co  m
    os.close();
    System.out.println(msg + Long.toString(System.currentTimeMillis() - currentTime));
}

From source file:Main.java

public static void close(Writer writer) {
    if (writer != null) {
        try {/*  w  w  w . j  a v a2 s.c  o m*/
            writer.close();
        } catch (IOException e) {
            closingFailed(e);
        }
    }
}

From source file:Main.java

public static void close(Writer closeable) {
    if (closeable != null)
        try {//from   w w  w  .  j  av  a2s . co m
            closeable.close();
        } catch (Exception ignored) {
        }
}

From source file:Main.java

public static final void close(final Writer out) {
    if (out != null) {
        try {/*w ww  . j a v a  2  s.co  m*/
            out.close();
        } catch (IOException e) {
            // silent
        }
    }
}

From source file:Main.java

public final static void closeQuietly(Writer writer) {
    try {/*from w  w  w  .ja  v  a  2s.  co m*/
        if (writer != null)
            writer.close();
    } catch (Exception e) {
        Log.e("OpenVPN", "closing Writer", e);
    }
}

From source file:Main.java

static void writeUtf(String text, String file) {
    Writer out;
    try {/*from   w  ww  .  ja v  a2  s . c  o  m*/
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        out.write(text);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Save {@link String} to {@link File} witht the specified encoding
 *
 * @param string {@link String}/*from www  . j a v a  2s  .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();
    }
}

From source file:Main.java

/**
 * Formats a file nice looking//from w  w  w  .j ava2s. c  o  m
 * @param file The XML file to format
 * @throws Exception If the file isn't an XML file
 */
public static void format(File file) throws Exception {
    if (file.isFile()) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;
        builder = factory.newDocumentBuilder();
        document = builder.parse(file);

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        Writer out = new FileWriter(file);
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);
        out.close();
    }
}

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

/**
 * Exports a JFreeChart to a SVG file./*w  w  w  . j a  v a  2 s . 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.github.rnewson.couchdb.lucene.util.ServletUtils.java

public static void sendJsonSuccess(final HttpServletRequest req, final HttpServletResponse resp)
        throws IOException {
    setResponseContentTypeAndEncoding(req, resp);
    final Writer writer = resp.getWriter();
    try {/*  w  ww . j  a v a 2s .c om*/
        writer.write("{\"ok\": true}\r\n");
    } finally {
        writer.close();
    }
}