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:Main.java

public static void saveXml(Document modDoc, String path) throws TransformerException, IOException {
    Writer writer = null;
    try {//from  w  w  w. j a  v a2  s  . co  m
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(modDoc);
        writer = getFileWriter(path);
        Result dest = new StreamResult(writer);

        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
        aTransformer.transform(src, dest);
    } catch (TransformerException e) {
        throw e;
    } finally {

        writer.close();
    }

}

From source file:no.digipost.android.utilities.JSONUtilities.java

public static String getJsonStringFromInputStream(final InputStream inputStream) {
    String content = "";

    if (inputStream != null) {
        Writer writer = new StringWriter();
        int buffer_size = 1024;
        char[] buffer = new char[buffer_size];

        try {//  ww  w .j ava 2 s. c  o m
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, ApiConstants.ENCODING),
                    buffer_size);
            int n;

            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            inputStream.close();
            reader.close();
            writer.close();
        } catch (Exception e) {
        }
        content = writer.toString();
    }
    return content;
}

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

/**
 *  Returns the full contents of the Reader as a String.
 *
 *  @since 1.5.8/*from w w w  .ja  v  a  2s  .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) {
            LOG.error("Not able to close the stream while reading contents.");
        }
    }
}

From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java

public static void createNewFileWithContent(IOEncodingHelper encodingHelper, final File file,
        final String content) throws Exception {

    if (encodingHelper == null) {
        encodingHelper = IOEncodingHelper.STANDARD;
    }/*from w ww  .j a va  2  s. co m*/

    if (file == null) {
        throw new IllegalArgumentException("FileUtil: file must not be null");
    }

    createNewFile(file);

    Writer writer = null;
    try {
        writer = encodingHelper.getOutputStreamWriter(file);
        writer.write(content);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file: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 ww  w . ja  v  a  2s .  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:com.ibm.jaql.lang.expr.system.RUtil.java

public static void doCleanup(java.io.Writer writer, String file) throws IOException {
    writer.close();
    File path = new File(file);
    if (path.exists())
        path.delete();//from www . ja va2 s.com
}

From source file:dataGen.DataGen.java

/**
 * Save the Date with the FileWriter/* w  ww .j a v  a2  s. c o m*/
 * @param values
 * @param fw
 * @param nf
 * @throws IOException
 */
public static void saveData(TupleList values, Writer fw, NumberFormat nf) throws IOException {
    for (int i = 0; i < values.size(); i++) {
        String row = i + 1 + " ";
        //String row = "";

        for (int j = 0; j < values.get(i).size(); j++) {
            row = row + nf.format(values.get(i).getValue(j)) + " ";
        }
        fw.write(row);
        fw.append(System.getProperty("line.separator"));
    }
    fw.close();
}

From source file:com.twinsoft.convertigo.engine.util.CarUtils.java

private static void exportXMLProject(String fileName, Document document) throws EngineException {
    try {//www  .  java  2s .c o  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        boolean isCR = FileUtils.isCRLF();

        Writer writer = isCR ? new StringWriter() : new FileWriterWithEncoding(fileName, "UTF-8");
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        if (isCR) {
            String content = FileUtils.CrlfToLf(writer.toString());
            writer = new FileWriterWithEncoding(fileName, "UTF-8");
            writer.write(content);
        }

        writer.close();
    } catch (Exception e) {
        throw new EngineException("(CarUtils) exportProject failed", e);
    }
}

From source file:net.sf.nmedit.jsynth.clavia.nordmodular.utils.NmUtils.java

public static void writePatch(NMPatch patch, OutputStream out) throws IOException, ParseException {
    Writer writer = new BufferedWriter(new OutputStreamWriter(out, getPatchFileCharset()));
    try {/*from w  w w . j  av a2s . com*/
        (new PatchExporter()).export(patch, new PatchFileWriter(writer));
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:it.unimi.di.big.mg4j.document.DocumentCollectionTest.java

@BeforeClass
public static void setUp() throws IOException, ConfigurationException {
    // Create a new directory under /tmp
    tempDir = File.createTempFile("mg4jtest", null);
    tempDir.delete();/*  ww  w  .j a va 2s.  c  om*/
    tempDir.mkdir();
    // Now create the hierarchy for HTML files
    File htmlDir = new File(tempDir, "html");
    htmlDir.mkdir();
    System.err.println("Temporary directory: " + tempDir);
    htmlFileSet = new String[ndoc];
    for (int i = 0; i < ndoc; i++) {
        String docFile = new File(htmlDir, "doc" + i + ".html").toString();
        htmlFileSet[i] = docFile;
        Writer docWriter = new OutputStreamWriter(new FileOutputStream(docFile), "ISO-8859-1");
        docWriter.write(getHTMLDocument(document[i]));
        docWriter.close();
    }
    // Now create the mbox file
    Writer mboxWriter = new OutputStreamWriter(new FileOutputStream(new File(tempDir, "mbox")), "ISO-8859-1");
    for (int i = 0; i < ndoc; i++)
        mboxWriter.write(getMboxDocument(document[i]));
    mboxWriter.close();

    // Now create the zip collections
    FileSetDocumentCollection fileSetDocumentCollection = new FileSetDocumentCollection(htmlFileSet,
            new HtmlDocumentFactory(DEFAULT_PROPERTIES));
    ZipDocumentCollectionBuilder zipCollBuilder = new ZipDocumentCollectionBuilder(
            new File(tempDir, "zip").toString(), fileSetDocumentCollection.factory(), true);
    zipCollBuilder.build(fileSetDocumentCollection);

    ZipDocumentCollectionBuilder apprZipCollBuilder = new ZipDocumentCollectionBuilder(
            new File(tempDir, "azip").toString(), fileSetDocumentCollection.factory(), false);
    apprZipCollBuilder.build(fileSetDocumentCollection);
    fileSetDocumentCollection.close();

    // Now create the simple collections
    SimpleCompressedDocumentCollectionBuilder simpleCollBuilder = new SimpleCompressedDocumentCollectionBuilder(
            new File(tempDir, "simple").toString(), fileSetDocumentCollection.factory(), true);
    simpleCollBuilder.build(fileSetDocumentCollection);

    SimpleCompressedDocumentCollectionBuilder apprSimpleCollBuilder = new SimpleCompressedDocumentCollectionBuilder(
            new File(tempDir, "asimple").toString(), fileSetDocumentCollection.factory(), false);
    apprSimpleCollBuilder.build(fileSetDocumentCollection);
    fileSetDocumentCollection.close();
}