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:es.tena.foundation.util.POIUtil.java

public static void generateXLS(String tabla, String filename, Connection conn, String encoding)
        throws SQLException {
    String query = "";
    try {/*  w  w  w .jav  a 2  s  .c o  m*/
        query = "SELECT * FROM (" + tabla + ")";
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rset = stmt.executeQuery();

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(filename);
        String sheetRef = sheet.getPackagePart().getPartName().getName();
        String template = "c:\\temp\\template_" + filename + ".xlsx";
        FileOutputStream os = new FileOutputStream(template);
        wb.write(os);
        os.close();

        File tmp = File.createTempFile("sheet", ".xml");
        Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), encoding);
        generate(fw, rset, encoding);
        rset.close();
        stmt.close();
        fw.close();

        FileOutputStream out = new FileOutputStream(
                "c:\\temp\\" + filename + sdf.format(calendario.getTime()) + ".xlsx");
        FileUtil.substitute(new File(template), tmp, sheetRef.substring(1), out);
        out.close();
        Logger.getLogger(POIUtil.class.getName()).log(Level.INFO, "Creado con exito {0}", filename);
    } catch (Exception ex) {
        ex.printStackTrace();
        Logger.getLogger(POIUtil.class.getName()).log(Level.SEVERE, null, query + "\n" + ex);
        System.out.println(query);
    } finally {
        conn.close();
    }
}

From source file:FileUtil.java

/**
 *  Returns the full contents of the Reader as a String.
 *
 *  @since 1.5.8/*from  w  w  w.  j a  v  a2  s. c  o  m*/
 *  @param in The reader from which the contents shall be read.
 *  @return String read from the Reader
 *  @throws IOException If reading fails.
 */
public static String readContents(Reader in) throws IOException {
    Writer out = null;

    try {
        out = new StringWriter();

        copyContents(in, out);

        return out.toString();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
            System.out.println("Not able to close the stream while reading contents.");
        }
    }
}

From source file:org.crazydog.util.spring.FileCopyUtils.java

/**
 * Copy the contents of the given String to the given output Writer.
 * Closes the writer when done.//from w ww  .ja  v a2s  .  c o  m
 * @param in the String to copy from
 * @param out the Writer to copy to
 * @throws IOException in case of I/O errors
 */
public static void copy(String in, Writer out) throws IOException {
    org.springframework.util.Assert.notNull(in, "No input String specified");
    org.springframework.util.Assert.notNull(out, "No Writer specified");
    try {
        out.write(in);
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
        }
    }
}

From source file:gdv.xport.util.AbstractFormatterTest.java

/**
 * Tested die Formattierung der Musterdatei als HTML.
 *
 * @param formatter the formatter//w  ww . j  a  va2  s.c om
 * @param filename the filename
 * @throws IOException falls was schiefgelaufen ist
 * @throws XMLStreamException falls was schiefgelaufen ist
 */
protected static void exportMusterdatei(final AbstractFormatter formatter, final String filename)
        throws IOException, XMLStreamException {
    Datenpaket datenpaket = new Datenpaket();
    InputStream istream = AbstractFormatterTest.class.getResourceAsStream("/musterdatei_041222.txt");
    File siteDir = new File("target", "site");
    if (siteDir.mkdirs()) {
        log.info("created: " + siteDir);
    }
    File exportFile = new File(siteDir, filename);
    Writer writer = new OutputStreamWriter(new FileOutputStream(exportFile), "ISO-8859-1");
    try {
        datenpaket.importFrom(istream);
        formatter.setWriter(writer);
        formatter.write(datenpaket);
        log.info(datenpaket + " exported to " + exportFile);
    } finally {
        writer.close();
        istream.close();
    }
}

From source file:de.ingrid.portal.global.UtilsFileHelper.java

/**
 * Write String into file//from  www.  j a  va2  s.c om
 * 
 * @param path
 * @param content
 * @throws IOException
 */
public static void writeContentIntoFile(String path, String content) throws IOException {
    if (path != null) {
        File file = new File(path);
        if (file.length() < 1) {
            if (content != null) {
                Writer writer = null;
                writer = new FileWriter(path);
                writer.write(content);
                writer.close();
            } else {
                if (log.isErrorEnabled()) {
                    log.error("Content is null!");
                }
            }
        }
    }
}

From source file:Main.java

public static void formatXML(Reader xml, Reader xsl, URIResolver resolver, Writer output) throws Exception {
    try {//ww w. j  a va2s .  c om
        try {
            try {
                Source xmlSource = new SAXSource(new InputSource(xml));
                Source xslSource = new SAXSource(new InputSource(xsl));
                Result result = new StreamResult(output);
                formatXML(xmlSource, xslSource, resolver, result);
            } finally {
                output.close();
            }
        } finally {
            xsl.close();
        }
    } finally {
        xml.close();
    }
}

From source file:bs.ws1.dm.itg.App.java

private static void writeToFile(String output, String filename) {
    Writer writer = null;
    try {//from   www .j av  a  2 s .  c  om
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename + ".csv"), "utf-8"));
        writer.write(output);
    } catch (IOException ex) {
        // report
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.feilong.commons.core.io.IOWriteUtil.java

/**
 * ./*from w w w  . j a v  a2 s.c  om*/
 * 
 * <ul>
 * <li>?,, (?? )</li>
 * <li>,?  {@link FileWriteMode#APPEND}??</li>
 * </ul>
 *
 * @param filePath
 *            
 * @param content
 *            
 * @param encode
 *            ?,isNullOrEmpty, {@link CharsetType#GBK}? {@link CharsetType}
 * @param fileWriteMode
 *            ?
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @throws IllegalArgumentException
 *             <ul>
 *             <li>filePath,isDirectory</li>
 *             <li>filePath,!canWrite</li>
 *             </ul>
 * @see FileWriteMode
 * @see CharsetType
 * @see com.feilong.commons.core.io.FileUtil#cascadeMkdirs(String)
 * @see java.io.FileOutputStream#FileOutputStream(File, boolean)
 */
public static void write(String filePath, String content, String encode, FileWriteMode fileWriteMode)
        throws UncheckedIOException, IllegalArgumentException {

    if (Validator.isNullOrEmpty(encode)) {
        encode = CharsetType.GBK;
    }

    // **************************************************************************8
    File file = FileUtil.cascadeMkdirs(filePath);

    boolean append = (fileWriteMode == FileWriteMode.APPEND);

    try {
        OutputStream outputStream = new FileOutputStream(file, append);
        Writer outputStreamWriter = new OutputStreamWriter(outputStream, encode);

        Writer writer = new PrintWriter(outputStreamWriter);
        writer.write(content);
        writer.close();

        if (log.isInfoEnabled()) {
            log.info("fileWriteMode:[{}],contentLength:[{}],encode:[{}],fileSize:[{}],absolutePath:[{}]",
                    fileWriteMode, content.length(), encode, FileUtil.getFileFormatSize(file),
                    file.getAbsolutePath());
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.openedit.util.FileUtils.java

public static void safeClose(Writer inIn) {
    if (inIn != null) {
        try {//w  w w .  ja va  2 s .c om
            inIn.close();
        } catch (IOException ex) {
            log.error(ex);
        }
    }
}

From source file:de.uzk.hki.da.sb.Utilities.java

/**
 * Writes the given text string into a file
 * /*from w w w . j a  v a 2s . co m*/
 * @param output File The file to write into
 * @param text The text to write into the file
 * @throws Exception
 */
public static void writeFile(File outputFile, String text) throws Exception {

    Writer writer;
    try {
        writer = new FileWriter(outputFile);
    } catch (IOException e) {
        throw new Exception("Couldn't create writer", e);
    }

    try {
        writer.write(text);
        writer.close();

    } catch (IOException e) {
        throw new Exception("Couldn't write to file " + outputFile.getAbsolutePath(), e);
    }
}