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:com.thruzero.common.core.utils.FileUtilsExt.java

/**
 * If {@code writer} not {@code null}, close it and ignore {@code IOException}
 *
 * @param reader// ww w  . j a  va2  s  .  co m
 */
public static void closeWriter(final Writer writer) {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            // ignore IOException
        }
    }
}

From source file:Main.java

public static <T> String obj2Xml(T c) throws RuntimeException {
    String returnXml = "";
    Writer writer = null;
    try {/*  w ww . j a v  a2s  .  c  o  m*/
        writer = new StringWriter();
        JAXBContext.newInstance(c.getClass()).createMarshaller().marshal(c, writer);
        returnXml = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return returnXml;
}

From source file:Main.java

/**
 * Writes out the contents./*from  w w w  .j a  v a  2  s  .co  m*/
 * @param outFile outFile
 * @param contents contents
 * @throws FileNotFoundException FileNotFoundException
 * @throws IOException IOException
 */
public static void setContents(final File outFile, final String contents)
        throws FileNotFoundException, IOException {
    if (outFile == null) {
        throw new IllegalArgumentException("File should not be null."); //$NON-NLS-1$
    }
    if (outFile.exists()) {
        if (!outFile.isFile()) {
            throw new IllegalArgumentException("Should not be a directory: " + outFile); //$NON-NLS-1$
        }
        if (!outFile.canWrite()) {
            throw new IllegalArgumentException("File cannot be written: " + outFile); //$NON-NLS-1$
        }
    }

    Writer output = null;
    try {
        output = new BufferedWriter(new FileWriter(outFile));
        if (contents != null) {
            output.write(contents);
            output.flush();
        }

    } finally {
        if (output != null) {
            output.close();
        }
    }
}

From source file:gr.abiss.calipso.util.XmlUtils.java

/**
 * Removes XSS threats from input HTML//www . j a va 2 s.  c  om
 *
 * @param s   the String to transform
 * @return    the transformed String
 */
public static String removeXss(String s) {
    if (s == null) {
        return null;
    }
    Writer sw = new StringWriter(); // ignore output
    try {
        DeXSS xss = DeXSS.createInstance(null, sw);
        xss.process(s);
        sw.close();
        s = sw.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    //System.out.println("Output HTML: \n"+s);
    return s;
}

From source file:Main.java

public static void formatXML(Document xml, Document xsl, URIResolver resolver, Writer output) throws Exception {
    try {//from www .j  a v  a2 s .  c om
        DOMSource xslSource = new DOMSource(xsl);
        DOMSource xmlSource = new DOMSource(xml);
        Result result = new StreamResult(output);
        formatXML(xmlSource, xslSource, resolver, result);
    } finally {
        output.close();
    }
}

From source file:com.joey.Fujikom.generate.Generate.java

/**
 * //from  w w w.j  a v a2s .c  om
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            FileOutputStream fos = new FileOutputStream(filePath);
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(content);
            bufferedWriter.close();
            writer.close();
        } else {
            logger.info("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java

/**
 * Exports a JFreeChart to a SVG file.//from  w w  w .j  av a  2s  .co  m
 *
 * @param chart
 *            JFreeChart to export
 * @param bounds
 *            the dimensions of the viewport
 * @param svgFile
 *            the output file.
 * @throws IOException
 *             if writing the svgFile fails.
 */
public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    try {
        Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation");

        // Get a DOMImplementation and create an XML document
        Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]);
        DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]);

        org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);

        // Create an instance of the SVG Generator
        Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D");
        Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class });
        Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class });
        Object svgGenerator = SG2DConstr.newInstance(document);

        // draw the chart in the SVG generator
        chart.draw((Graphics2D) svgGenerator, bounds);

        // Write svg file
        OutputStream outputStream = new FileOutputStream(svgFile);
        Writer out = new OutputStreamWriter(outputStream, "UTF-8");
        streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ });
        outputStream.flush();
        outputStream.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.ibm.jaggr.core.util.CopyUtil.java

public static int copy(Reader reader, Writer writer) throws IOException {
    try {/*from   w  w w  .ja v  a2s . c  o  m*/
        return IOUtils.copy(reader, writer);
    } finally {
        try {
            reader.close();
        } catch (Exception ignore) {
        }
        try {
            writer.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:ca.simplegames.micro.utils.IO.java

/**
 * Close the given stream if the stream is not null.
 *
 * @param s The stream/*from www.jav a  2  s  .  c o m*/
 */

public static void close(Writer s) {
    if (s != null) {
        try {
            s.close();
        } catch (Exception e) {
            log.error("Error closing writer: " + e.getMessage());
        }
    }
}

From source file:de.bund.bfr.knime.pmm.common.chart.ChartUtilities.java

public static void saveChartAs(JFreeChart chart, String fileName, int width, int height) {
    if (fileName.toLowerCase().endsWith(".png")) {
        try {//from w w w.j a v a  2  s  .co  m
            org.jfree.chart.ChartUtilities.writeChartAsPNG(new FileOutputStream(fileName), chart, width,
                    height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (fileName.toLowerCase().endsWith(".svg")) {
        try {
            DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
            Document document = domImpl.createDocument(null, "svg", null);
            SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
            Writer outsvg = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);

            chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
            svgGenerator.stream(outsvg, true);
            outsvg.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}