Example usage for java.io FileWriter close

List of usage examples for java.io FileWriter close

Introduction

In this page you can find the example usage for java.io FileWriter close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:org.openmrs.module.pcslabinterface.PcsLabInterfaceUtil.java

public static void stringToFile(String fileContents, File outFile) throws IOException {
    FileWriter writer = new FileWriter(outFile);

    writer.write(fileContents);//from   w  w  w. j a  v a  2s.c  om

    writer.close();
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditingTestsUtils.java

static void writeToFile(File file, String fileContent) {
    try {// w ww .jav  a  2 s.c o  m
        FileWriter writer = new FileWriter(file);
        writer.write(fileContent);
        writer.flush();
        writer.close();
    } catch (Exception exception) {
        throw new AuditingException(exception, "Error writing content to file '%s'.", file.getAbsolutePath());
    }
}

From source file:mlbench.pagerank.PagerankMerge.java

private static void reduceDiffs(int local_diffs, int rank) {
    int diffs[] = { 0 };
    int bs[] = { local_diffs };
    try {//from  w w  w . j  a va 2 s. c om
        MPI.COMM_WORLD.Reduce(bs, 0, diffs, 0, 1, MPI.INT, MPI.SUM, 0);
        MPI.COMM_WORLD.Barrier();
        if (rank == 0) {
            LOG.info("Uncoveraged diffs:" + diffs[0]);
            FileWriter output = new FileWriter("var.tmp");
            output.write(String.valueOf(diffs[0]));
            output.close();
            JobConf conf = new JobConf(confPath);
            final FileSystem fs = FileSystem.get(conf);
            fs.copyFromLocalFile(true, new Path("./var.tmp"), new Path("var.tmp"));
        }

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

From source file:Main.java

public static void writeToFile(Collection<?> collection, File file) throws IOException {
    if (collection != null && file != null) {
        file.getParentFile().mkdirs();//from www .  j  a va2s .com
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (Object obj : collection) {
            fw.write(new StringBuilder(obj.toString()).append("\r\n").toString());
        }
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:com.daphne.es.maintain.icon.web.controller.tmp.GenCssSql.java

private static void readUploadFile() throws IOException {
    String fromFile = "E:\\workspace\\git\\es\\web\\src\\main\\webapp\\WEB-INF\\static\\comp\\zTree\\css\\zTreeStyle\\img\\diy\\icon.txt";
    String toFile = "C:\\Documents and Settings\\Administrator\\?\\b.sql";
    String template = "insert into `maintain_icon` (`id`, `identity`, `img_src`, `type`, `width`, `height`) values(%1$d, '%2$s', '%3$s', 'upload_file', %4$d, %5$d);";

    List<String> list = FileUtils.readLines(new File(fromFile));
    FileWriter writer = new FileWriter(toFile);

    int count = 300;
    for (int i = 0, l = list.size(); i < l; i += 2) {
        writer.write(String.format(template, count++, list.get(i), list.get(i + 1), 16, 16));
        writer.write("\r\n");
    }/*from w w w  .  j a  v  a  2 s  .  c  om*/

    writer.close();
}

From source file:Main.java

public static void writeToFile(Iterator<?> iter, File file) throws IOException {
    if (iter != null && file != null) {
        file.getParentFile().mkdirs();/* ww w  . ja v a  2s  . c om*/
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (; iter.hasNext();) {
            fw.write(new StringBuilder(iter.next().toString()).append("\r\n").toString());
        }
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:tools.EpsServletUtilities.java

public static String saveChartAsEPS(JFreeChart chart, int width, int height, HttpSession session)
        throws IOException {

    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }/*from   ww  w  .  ja v a  2s.c  o  m*/
    EpsServletUtilities.createTempDir();
    String prefix = EpsServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = EpsServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".eps", new File(System.getProperty("java.io.tmpdir")));

    EpsGraphics2D graphic2d = new EpsGraphics2D();
    chart.draw(graphic2d, new Rectangle2D.Double(0, 0, width, height));

    try {
        FileWriter fos = new FileWriter(tempFile);
        fos.write(graphic2d.toString());
        fos.close();

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    if (session != null) {
        EpsServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getPath();

}

From source file:com.eucalyptus.gen2ools.TemplateGenerator.java

private static void writeTemplate(VelocityContext context, String templateName, File outFile) {
    if (!outFile.getParentFile().exists()) {
        outFile.getParentFile().mkdirs();
    }/*from  ww  w  .  java  2 s . c o  m*/
    Template template = null;
    try {
        template = Velocity.getTemplate(templateName);
        FileWriter out = new FileWriter(outFile);
        template.merge(context, out);
        out.flush();
        out.close();
    } catch (ResourceNotFoundException ex) {
        // couldn't find the template
        LOG.error(ex, ex);
    } catch (ParseErrorException ex) {
        // syntax error: problem parsing the template
        LOG.error(ex, ex);
    } catch (MethodInvocationException ex) {
        // something invoked in the template
        // threw an exception
        LOG.error(ex, ex);
    } catch (IOException ex) {
        LOG.error(ex, ex);
    }
}

From source file:com.mycompany.myelasticsearch.DocumentReader.java

public static void getAllCapitalWords(String docText) {
    Set<String> allCapsWords = new HashSet<>();
    Pattern p = Pattern.compile("\\b[A-Z]{2,}\\b");
    Matcher m = p.matcher(docText);
    while (m.find()) {
        String word = m.group();//from   www  .ja  v a 2 s. co  m
        // System.out.println(word);
        allCapsWords.add(word);
    }

    for (String allcaps : allCapsWords) {
        System.out.println(allcaps);
    }
    System.out.println("Caps word count" + allCapsWords.size());
    org.json.simple.JSONObject obj = new org.json.simple.JSONObject();
    int count = 0;
    for (String output : outputArray) {
        obj.put(String.valueOf(count), output.replaceAll("\\s+", " "));
        count++;
    }
    try {

        FileWriter file = new FileWriter("CapsWord.txt");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

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

From source file:com.espe.distribuidas.pmaldito.servidorbdd.operaciones.Archivo.java

/**
 * Permite insertar registros en una tabla
 *
 * @param string/*from w  w  w .  ja  v a 2s  . c o m*/
 * @param archivo
 */
@SuppressWarnings("ConvertToTryWithResources")
public static void insertar(String string, File archivo) {

    try {
        FileWriter fw = new FileWriter(archivo, true);
        fw.write(string + "\n");
        fw.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}