Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:com.netflix.config.DynamicFileConfigurationTest.java

static void modifyConfigFile() {
    new Thread() {
        public void run() {
            try {
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
                writer.write("abc=-2"); // this property should fail validation but should not affect update of other properties
                writer.newLine();//from  w ww.j  av a  2  s.co m
                writer.write("dprops1=" + String.valueOf(Long.MIN_VALUE));
                writer.newLine();
                writer.write("dprops2=" + String.valueOf(Double.MAX_VALUE));
                writer.newLine();
                writer.close();
                System.err.println(configFile.getPath() + " modified");
            } catch (Exception e) {
                e.printStackTrace();
                fail("Unexpected exception");
            }
        }
    }.start();
}

From source file:cn.org.citycloud.srdz.utils.FileUtils.java

/**
 * /* www  . j  ava2 s.  co m*/
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            FileWriter fileWriter = new FileWriter(filePath, true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.close();
            fileWriter.close();
        } else {
            log.info("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.util.FileUtil.java

public static void write(File file, String s) throws IOException {
    if (file.getParent() != null) {
        mkdirs(file.getParent());//www  .  j a  v a 2  s  .  c o  m
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);

    bw.close();
}

From source file:ac.ucy.cs.spdx.license.License.java

/**
 * Saves the License passed as parameter in a text file inside the standard
 * directory./*from www. ja  v  a2  s. co m*/
 * 
 * @param License
 */
public static void saveLicense(License l) {
    File text = new File("licensesText/" + l.getIdentifier() + ".txt");
    if (text.exists())
        return;
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
        text.createNewFile();
        fw = new FileWriter(text);
        bw = new BufferedWriter(fw);
        bw.write(l.getLicenseName() + "\n");
        bw.write(l.getIdentifier() + "\n");
        bw.write(l.getLicenseText());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bw.close();
            fw.close();
        } catch (IOException e) {
        }

    }

}

From source file:com.dc.util.file.FileSupport.java

public static void writeTextFile(String folderPath, String fileName, String data, boolean isExecutable)
        throws IOException {
    File dir = new File(folderPath);
    if (!dir.exists()) {
        dir.mkdirs();//from  w w w  .  j a  va  2 s  . c  o m
    }
    File file = new File(dir, fileName);
    FileWriter fileWriter = null;
    BufferedWriter bufferedWriter = null;
    try {
        fileWriter = new FileWriter(file);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(data);
        bufferedWriter.flush();
        if (isExecutable) {
            file.setExecutable(true);
        }
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
        if (bufferedWriter != null) {
            bufferedWriter.close();
        }
    }
}

From source file:entity.files.SYSFilesTools.java

public static File getHtmlFile(String html, String prefix, String ext) {
    File temp = null;/*from  w  w  w  .ja  v a  2s. co  m*/
    try {
        // Create temp file.
        temp = File.createTempFile(prefix, ext);

        String text = "<html><head>";

        text += OPDE.getCSS();
        text += "</head><body>" + SYSTools.htmlUmlautConversion(html) + "<hr/>" + "<div id=\"fonttext\">"
                + "<b>" + SYSTools.xx("misc.msg.endofreport") + "</b><br/>"
                + (OPDE.getLogin() != null ? SYSTools.htmlUmlautConversion(OPDE.getLogin().getUser().getUID())
                        : "")
                + "<br/>" + DateFormat.getDateTimeInstance().format(new Date()) + "<br/>"
                + OPDE.getAppInfo().getProgname() + ", v" + OPDE.getAppInfo().getVersion() + "/"
                + OPDE.getAppInfo().getBuildnum() + "</div></body></html>";

        // Write to temp file
        BufferedWriter out = new BufferedWriter(new FileWriter(temp));
        out.write(text);

        out.close();
    } catch (IOException e) {
        OPDE.error(e);
    }
    return temp;
}

From source file:it.unibas.spicybenchmark.persistence.DAOLogFile.java

public static void saveLog(String log, String logFile) throws DAOException {
    BufferedWriter writer = null;
    try {/*from  w  w  w  .  j  a v  a2s . c  o m*/
        File f = new File(logFile);
        f.getParentFile().mkdirs();
        FileWriter fileWriter = new FileWriter(logFile);
        writer = new BufferedWriter(fileWriter);
        writer.write(log);
    } catch (FileNotFoundException fnfe) {
        throw new DAOException(" File not found: " + fnfe);
    } catch (IOException ioe) {
        throw new DAOException(" Error: " + ioe);
    } catch (NoSuchElementException nse) {
        throw new DAOException(" Error in file format: " + nse);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:madkitgroupextension.export.Export.java

public static void saveAndIncrementBuild() throws IOException {
    MadKitGroupExtension.VERSION.setBuildNumber(MadKitGroupExtension.VERSION.getBuildNumber() + 1);
    FileWriter fw = new FileWriter(new File("build.txt"));
    BufferedWriter b = new BufferedWriter(fw);
    b.write(Integer.toString(MadKitGroupExtension.VERSION.getBuildNumber()));
    b.flush();//from w ww  . ja  v  a 2s .  c om
    b.close();
    fw.close();
}

From source file:it.tizianofagni.sparkboost.DataUtils.java

/**
 * Write a text file on Hadoop file system by using standard Hadoop API.
 *
 * @param outputPath The file to be written.
 * @param content    The content to put in the file.
 *//*  w w w . ja  v a 2 s  .c o  m*/
public static void saveHadoopTextFile(String outputPath, String content) {
    try {
        Configuration configuration = new Configuration();
        Path file = new Path(outputPath);
        Path parentFile = file.getParent();
        FileSystem hdfs = FileSystem.get(file.toUri(), configuration);
        if (parentFile != null)
            hdfs.mkdirs(parentFile);
        OutputStream os = hdfs.create(file, true);
        BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        br.write(content);
        br.close();
        hdfs.close();
    } catch (Exception e) {
        throw new RuntimeException("Writing Hadoop text file", e);
    }
}

From source file:com.gargoylesoftware.htmlunit.source.JQuery173Extractor.java

/**
 * Transforms the raw expectation, to the needed one by HtmlUnit.
 * This methods puts only the main line of the test output, without the details.
 *
 * @param input the raw file of real browser, with header and footer removed
 * @param output the expectation//from w ww .j a  v  a2  s . c  o  m
 * @throws IOException if an error occurs
 */
public static void extractExpectations(final File input, final File output) throws IOException {
    final BufferedReader reader = new BufferedReader(new FileReader(input));
    final BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    int testNumber = 1;
    String line;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("" + testNumber + '.') && line.endsWith("Rerun")) {
            line = line.substring(0, line.length() - 5);
            writer.write(line + "\n");
            testNumber++;
        }
    }
    System.out.println("Last output #" + (testNumber - 1));
    reader.close();
    writer.close();
}