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:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

public static void writeChartToSVG(JFreeChart chart, int width, int height, File name) throws Exception {
    // Get a DOMImplementation
    DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
    org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));

    boolean useCSS = true; // we want to use CSS style attribute

    Writer out = null;
    try {// w  w w . j  a v a 2  s .  c om
        out = new OutputStreamWriter(new FileOutputStream(name), "UTF-8");
        svgGenerator.stream(out, useCSS);
    } catch (UnsupportedEncodingException | FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (SVGGraphics2DIOException e) {
        e.printStackTrace();
        throw e;
    } finally {
        try {
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }
    }
}

From source file:net.sf.jasperreports.util.CastorUtil.java

/**
 * @deprecated Replaced by {@link #writeToFile(Object, String)}.
 *//*ww  w  . jav  a  2s.  c  o  m*/
public static void write(Object object, Mapping mapping, File file) {
    Writer writer = null;

    try {
        writer = new FileWriter(file);
        write(object, mapping, writer);
    } catch (IOException e) {
        throw new JRRuntimeException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:net.sf.jasperreports.util.CastorUtil.java

/**
 * @deprecated Replaced by {@link #writeToFile(Object, String)}.
 *///  ww  w.ja  va 2  s .c o  m
public static void write(Object object, String mappingFile, File file) {
    Writer writer = null;

    try {
        writer = new FileWriter(file);
        write(object, mappingFile, writer);
    } catch (IOException e) {
        throw new JRRuntimeException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:eu.project.ttc.readers.TermSuiteJsonCasSerializer.java

public static void serialize(Writer writer, JCas jCas) throws IOException {

    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jg = jsonFactory.createGenerator(writer);
    jg.useDefaultPrettyPrinter();//from w w w.  j a  v a2  s. c om
    jg.writeStartObject();
    jg.writeFieldName(F_SDI);
    writeSDI(jg, jCas);
    jg.writeFieldName(F_WORD_ANNOTATIONS);
    writeWordAnnotations(jg, jCas);
    jg.writeFieldName(F_TERM_OCC_ANNOTATIONS);
    writeTermOccAnnotations(jg, jCas);
    jg.writeFieldName(F_FIXED_EXPRESSIONS);
    writeFixedExpressions(jg, jCas);
    writeCoveredText(jg, jCas);
    jg.writeEndObject();
    jg.flush();
    writer.close();
}

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

/**
 * Copy the contents of the given Reader to the given Writer.
 * Closes both when done.//from  www  .  j a  v  a2 s  .  c  om
 * @param in the Reader to copy from
 * @param out the Writer to copy to
 * @return the number of characters copied
 * @throws IOException in case of I/O errors
 */
public static int copy(Reader in, Writer out) throws IOException {
    org.springframework.util.Assert.notNull(in, "No Reader specified");
    org.springframework.util.Assert.notNull(out, "No Writer specified");
    try {
        int byteCount = 0;
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
        }
        try {
            out.close();
        } catch (IOException ex) {
        }
    }
}

From source file:com.uber.hoodie.hive.TestUtil.java

private static HoodieLogFile generateLogData(Path parquetFilePath, boolean isLogSchemaSimple)
        throws IOException, InterruptedException, URISyntaxException {
    Schema schema = (isLogSchemaSimple ? SchemaTestUtil.getSimpleSchema() : SchemaTestUtil.getEvolvedSchema());
    HoodieDataFile dataFile = new HoodieDataFile(fileSystem.getFileStatus(parquetFilePath));
    // Write a log file for this parquet file
    Writer logWriter = HoodieLogFormat.newWriterBuilder().onParentPath(parquetFilePath.getParent())
            .withFileExtension(HoodieLogFile.DELTA_EXTENSION).withFileId(dataFile.getFileId())
            .overBaseCommit(dataFile.getCommitTime()).withFs(fileSystem).build();
    List<IndexedRecord> records = (isLogSchemaSimple ? SchemaTestUtil.generateTestRecords(0, 100)
            : SchemaTestUtil.generateEvolvedTestRecords(100, 100));
    HoodieAvroDataBlock dataBlock = new HoodieAvroDataBlock(records, schema);
    logWriter.appendBlock(dataBlock);/*  w  w  w. j av a 2s . co m*/
    logWriter.close();
    return logWriter.getLogFile();
}

From source file:net.duckling.ddl.util.FileUtil.java

/**
 *  Makes a new temporary file and writes content into it. The temporary
 *  file is created using <code>File.createTempFile()</code>, and the usual
 *  semantics apply.  The files are not deleted automatically in exit.
 *
 *  @param content Initial content of the temporary file.
 *  @param encoding Encoding to use.//from  w w w.  jav a  2  s  .co m
 *  @return The handle to the new temporary file
 *  @throws IOException If the content creation failed.
 *  @see java.io.File#createTempFile(String,String,File)
 */
public static File newTmpFile(String content, String encoding) throws IOException {
    Writer out = null;
    Reader in = null;
    File f = null;

    try {
        f = File.createTempFile("jspwiki", null);

        in = new StringReader(content);

        out = new OutputStreamWriter(new FileOutputStream(f), encoding);

        copyContents(in, out);
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

    return f;
}

From source file:de.iisys.schub.processMining.similarity.AlgoController.java

public static void saveToOutputFile(String output, String outputFileName) {
    Writer out;
    try {//from  ww w  . ja  v a 2s  .c o m
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "UTF-8"));
        try {
            out.write(output);
            System.out
                    .println("PROCESSMINING: Successfully saved smiliarity results to " + outputFileName + "!");
        } finally {
            out.close();
            out = null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/**
 * True if the write succeed otherwise it is false.
 * //from   www . ja  v a 2 s.co  m
 * @param file - File including full directory path to write.
 * @param contents - String complete contents of file.
 * @return boolean
 */
public static boolean write(File file, String contents) {
    try {
        Writer out = new BufferedWriter(new FileWriter(file));
        out.write(contents);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        log.warning("Failed to write file: " + file.getAbsolutePath(), e);
        return false;
    }
}

From source file:com.handany.base.generator.FreemarkerUtil.java

public static void outputProcessResult(String outputFile, Template template, Map<String, Object> varMap) {
    String resultString;//www .  jav a2  s .c o m
    ByteArrayOutputStream baos = null;
    Writer writer = null;

    try {
        baos = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(baos, CHARSET);
        template.process(varMap, writer);
        resultString = new String(baos.toByteArray(), CHARSET);
        FileUtils.writeStringToFile(new File(outputFile), resultString, CHARSET);
    } catch (UnsupportedEncodingException ex) {
        log.error(ex.getMessage(), ex);
    } catch (IOException | TemplateException ex) {
        log.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    } finally {
        if (null != baos) {
            try {
                baos.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }

        if (null != writer) {
            try {
                writer.close();
            } catch (IOException ex) {
                log.warn(ex.getMessage(), ex);
            }
        }
    }
}