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:cat.tv3.eng.rec.recomana.lupa.visualization.TextsResumeToJson.java

private static void saveResults(JSONArray recomendations, String id) {
    Writer out;
    try {/*w  w w.  j  ava 2  s  .c  o  m*/
        out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("data_toVisualize/data_resume/resume_" + id + ".json"), "UTF-8"));

        try {
            out.write(recomendations.toJSONString());
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.microsoft.tfs.util.IOUtils.java

/**
 * Unconditionally closes the specified {@link Writer}, safely handling any
 * {@link IOException} thrown by the {@link Writer#close()} method. If the
 * log for this class is properly configured, any such exception will be
 * logged, but it will never cause an exception to be thrown to the caller.
 * This method is typically called from a finally block in caller code.
 *
 * @param writer/*from w  ww  .  j a  v a2s  . co  m*/
 *        the {@link Writer} to close (must not be <code>null</code>)
 */
public static void closeSafely(final Writer writer) {
    Check.notNull(writer, "writer"); //$NON-NLS-1$

    try {
        writer.close();
    } catch (final IOException e) {
        if (log.isTraceEnabled()) {
            log.trace("error closing Writer", e); //$NON-NLS-1$
        }
    }
}

From source file:ilearnrw.utils.ServerHelperClass.java

private static void writeToFile(String outfilename, String data) throws Exception {
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfilename), "UTF-8"));
    try {//ww  w  . java2  s .  c o  m
        out.write(data);
    } finally {
        out.close();
    }
}

From source file:ca.uqac.dim.net.verify.NetworkChecker.java

/**
 * Runs the NuSMV program as a spawned command-line process, and passes the
 * file to process through that process' standard input
 * @param file_contents A String containing the NuSMV model to process
 *///from w  w w  .  java2s  .co  m
private static RuntimeInfos runNuSMV(String file_contents) throws IOException, InterruptedException {
    StringBuilder sb = new StringBuilder();
    Runtime rt = Runtime.getRuntime();
    long time_start = 0, time_end = 0;
    // Start NuSMV, and feed the model through its standard input
    time_start = System.currentTimeMillis();
    Process p = rt.exec(NUSMV_EXEC);
    OutputStream o = p.getOutputStream();
    InputStream i = p.getInputStream();
    InputStream e = p.getErrorStream();
    Writer w = new PrintWriter(o);
    w.write(file_contents);
    w.close(); // Close stdin so NuSMV can start processing it
    // Wait for NuSMV to be done, then collect its standard output
    int exitVal = p.waitFor();
    if (exitVal != 0)
        throw new IOException("NuSMV's return value indicates an error in processing its input");
    BufferedReader br = new BufferedReader(new InputStreamReader(i));
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line).append("\n");
    }
    time_end = System.currentTimeMillis();
    i.close(); // Close stdout
    e.close(); // Close stderr
    return new RuntimeInfos(sb.toString(), time_end - time_start);
}

From source file:com.asksven.commandcenter.valueobjects.CommandReaderWriter.java

public static void writeStream(CommandCollection data, OutputStream target) // throws Exception
{

    if (target == null) {
        return;//  w  ww. j  a  v  a2s.  c o m
    }
    Gson gson = new Gson();

    try {
        Writer writer = new OutputStreamWriter(target);

        // Now do the magic.
        gson.toJson(data, CommandCollection.class, writer);
        writer.close();
    } catch (Exception e) {
        Log.e(TAG, "Error while marshalling json file: " + e.getMessage());
    }
}

From source file:com.cloudbees.diff.UnifiedDiff.java

private static void copyStreamsCloseAll(Writer writer, Reader reader) throws IOException {
    IOUtils.copy(reader, writer);//from  www .  ja va 2 s .  co  m
    writer.close();
    reader.close();
}

From source file:com.redhat.rhn.common.util.FileUtils.java

/**
 * Save a String to a file on disk using specified path.
 *
 * WARNING:  This deletes the original file before it writes.
 *
 * @param contents to save to file on disk
 * @param path to save file to./*from  w  w  w .  j  a  v a2  s . c  o  m*/
 */
public static void writeStringToFile(String contents, String path) {
    try {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        Writer output = new BufferedWriter(new FileWriter(file));
        try {
            output.write(contents);
        } finally {
            output.close();
        }
    } catch (Exception e) {
        log.error("Error trying to write file to disk: [" + path + "]", e);
        throw new RuntimeException(e);
    }
}

From source file:heigit.ors.util.FileUtility.java

public static void writeFile(String fileName, String encoding, String content) throws IOException {
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), encoding));
    out.append(content);/* w  ww . j  av a2s  .c  om*/
    out.flush();
    out.close();
}

From source file:Main.java

public static void writeToFile(String s, String filePath, boolean append) {
    File f = new File(filePath);
    Writer writer = null;
    try {/*  w  w  w . java 2s  . com*/
        writer = new BufferedWriter(new FileWriter(f, append));
        writer.write(s);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:FileCopyUtils.java

/**
 * Copy the contents of the given String to the given output Writer.
 * Closes the write when done./*from w  w  w.jav  a 2  s .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 {

    try {
        out.write(in);
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
            System.out.println("Could not close Writer" + ex);
        }
    }
}