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:twitter4j.SpringCompatibilityTest.java

private void writeFile(String path, String content) throws IOException {
    File file = new File(path);
    file.delete();/* w w  w. ja v  a 2 s. c  o m*/
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    bw.write(content);
    bw.close();
}

From source file:com.almarsoft.GroundhogReader.lib.ServerManager.java

private static String saveMessageToOutbox(String fullMessage, Context context) {

    String error = null;//from ww w. j ava2 s  .  co m

    long outid = DBUtils.insertOfflineSentPost(context);

    String outputDir = UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME
            + "/offlinecache/outbox/";
    File outDir = new File(outputDir);
    if (!outDir.exists())
        outDir.mkdirs();

    File outFile = new File(outDir, Long.toString(outid));
    BufferedWriter out = null;

    try {
        FileWriter writer = new FileWriter(outFile);
        out = new BufferedWriter(writer);
        out.write(fullMessage);
        out.flush();
    } catch (IOException e) {
        error = e.getMessage();
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException e) {
        }
    }

    return error;
}

From source file:com.eleven0eight.xls2json.App.java

public void saveJson(String filename, String json) throws Exception {

    BufferedWriter out = new BufferedWriter(new FileWriter(filename));
    out.write(json);//w  w  w . j  av a2  s  . com
    out.close();

}

From source file:atg.tools.dynunit.test.util.FileUtil.java

public static void forceComponentScope(final File file, final String scope) throws IOException {
    final long oldLastModified = getConfigFileLastModified(file);
    if (oldLastModified < file.lastModified()) {
        final File tempFile = newTempFile();
        BufferedWriter out = null;
        BufferedReader in = null;
        try {//from w  w  w .ja va  2 s .com
            out = new BufferedWriter(new FileWriter(tempFile));
            in = new BufferedReader(new FileReader(file));
            for (String line = in.readLine(); line != null; line = in.readLine()) {
                if (line.contains("$scope")) {
                    out.write("$scope=");
                    out.write(scope);
                } else {
                    out.write(line);
                }
                out.newLine();
            }
            FileUtils.copyFile(tempFile, file);
            setConfigFileLastModified(file);
            dirty = true;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

From source file:bftsmart.tom.util.RSAKeyPairGenerator.java

private void saveToFile(int id, PublicKey puk, PrivateKey prk) throws Exception {
    String path = "config" + System.getProperty("file.separator") + "keys"
            + System.getProperty("file.separator");

    BufferedWriter w = new BufferedWriter(new FileWriter(path + "publickey" + id, false));
    w.write(getKeyAsString(puk));/*from   ww  w  . j a v  a  2s  .c  om*/
    w.flush();
    w.close();

    w = new BufferedWriter(new FileWriter(path + "privatekey" + id, false));
    w.write(getKeyAsString(prk));
    w.flush();
    w.close();
}

From source file:com.t_oster.visicut.misc.Helper.java

public static void installInkscapeExtension() throws FileNotFoundException, IOException {
    File src = new File(getVisiCutFolder(), "inkscape_extension");
    if (!src.exists() || !src.isDirectory()) {
        throw new FileNotFoundException("Not a directory: " + src);
    }// w ww  .j av a2  s .  c  o  m

    String profile_path = System.getenv("INKSCAPE_PORTABLE_PROFILE_DIR");

    if (profile_path == null) {
        profile_path = System.getenv("INKSCAPE_PROFILE_DIR");
    }

    File trg;

    if (profile_path != null) {
        trg = new File(profile_path);
    } else {
        if (isWindows()) {
            trg = new File(System.getenv("AppData"));
        } else {
            trg = new File(FileUtils.getUserDirectory(), ".config");
        }
    }

    trg = new File(new File(trg, "inkscape"), "extensions");

    if (!trg.exists() && !trg.mkdirs()) {
        throw new FileNotFoundException("Can't create directory: " + trg);
    }
    for (File f : src.listFiles()) {
        if ("visicut_export.py".equals(f.getName())) {
            File target = new File(trg, "visicut_export.py");
            BufferedReader r = new BufferedReader(new FileReader(f));
            BufferedWriter w = new BufferedWriter(new FileWriter(target));
            String line = r.readLine();
            while (line != null) {
                if ("VISICUTDIR=\"\"".equals(line)) {
                    line = "VISICUTDIR=r\"" + getVisiCutFolder().getAbsolutePath() + "\"";
                }
                w.write(line);
                w.newLine();
                line = r.readLine();
            }
            w.flush();
            w.close();
            r.close();
        } else if (f.getName().toLowerCase().endsWith("inx") || f.getName().toLowerCase().endsWith("py")) {
            FileUtils.copyFileToDirectory(f, trg);
        }
    }
}

From source file:csv.FileManager.java

static public void FetchMatchDetails(String toFileName, ArrayList<String> fromFileNames) {
    try {//from   w  ww  . j  a  v a2s. c  om
        ArrayList<ArrayList<String>> toWrite = new ArrayList<ArrayList<String>>();
        for (String fileName : fromFileNames) {

            BufferedReader in = new BufferedReader(new FileReader(fileName));
            CSVReader reader = new CSVReader(in);
            String[] nextLine;
            ArrayList<String> row = new ArrayList<String>();
            int championIndex = 0, index = 1;
            int countWinning = 0, countLosing = 0;
            while ((nextLine = reader.readNext()) != null) {

                for (int i = 0; i < nextLine.length; i++)
                    row.add(nextLine[i]);
                championIndex++;

                if (championIndex == 10) {
                    toWrite.add(row);
                    row = new ArrayList<String>();
                    championIndex = 0;
                    countWinning = 0;
                    countLosing = 0;
                }
                index++;
            }
            in.close();
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(toFileName));
        CSVWriter writer = new CSVWriter(out);
        ArrayList<String> header = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            header.add("champID_" + (i + 1) + "_player");
            header.add("item1_" + (i + 1) + "_player");
            header.add("item2_" + (i + 1) + "_player");
            header.add("item3_" + (i + 1) + "_player");
            header.add("item4_" + (i + 1) + "_player");
            header.add("item5_" + (i + 1) + "_player");
            header.add("item6_" + (i + 1) + "_player");
            header.add("item7_" + (i + 1) + "_player");
            header.add("won_" + (i + 1) + "_player");
        }
        toWrite.add(0, header);
        String[] t = new String[0];
        for (ArrayList<String> row : toWrite) {
            writer.writeNext(row.toArray(t));
        }
        out.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:model.settings.ReadSettings.java

/**
 * reads text from a configuration file; if is configuration file 
 * is not valid, return null; otherwise return configuration line.
 * /*  w  ww.ja  v a  2  s.c om*/
 * @param _path from which _path is red.
 * @return the text read form file @ _path.
 * 
 * @throws IOException is thrown in case of error.
 */
public static void changeOption(final String _operation, final String _newValue) throws IOException {

    //create Reader
    FileReader fr = new FileReader(PROGRAM_SETTINGS_LOCATION);
    BufferedReader br = new BufferedReader(fr);

    String sumLine = "";
    String currentLine = "";
    boolean found = false;
    currentLine = br.readLine();
    while (currentLine != null) {

        if (!found) {
            sumLine += currentLine + "\n";
        } else {
            found = !found;
            sumLine += _newValue + "\n";
        }

        // if the current line is the identifier of the current
        // operation that has to be changed.
        if (currentLine != null && currentLine.equals(_operation)) {
            found = true;
        }
        currentLine = br.readLine();

    }

    //close reader
    br.close();
    fr.close();

    FileWriter fw = new FileWriter(PROGRAM_SETTINGS_LOCATION);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(sumLine);
    bw.flush();
    bw.close();
    fw.close();
}

From source file:org.openengsb.openengsbplugin.tools.OpenEngSBVersionResolver.java

private File createTmpFile(String content) throws IOException {
    File temp = File.createTempFile("metadata", ".xml");
    temp.deleteOnExit();/*from ww w. j  a  va2s .  com*/
    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write(content);
    out.close();
    return temp;
}

From source file:org.jboss.as.test.manualmode.web.valve.authenticator.ValveUtil.java

public static void createValveModule(final ManagementClient managementClient, String modulename,
        String baseModulePath, String jarName, Class valveClass) throws Exception {
    log.info("Creating a valve module " + modulename);
    String path = ValveUtil.readASPath(managementClient.getControllerClient());
    File file = new File(path);
    if (file.exists()) {
        file = new File(path + baseModulePath);
        file.mkdirs();//from  ww  w.  ja v a 2 s.  c o m
        file = new File(path + baseModulePath + "/" + jarName);
        if (file.exists()) {
            file.delete();
        }
        createJar(file, valveClass);
        file = new File(path + baseModulePath + "/module.xml");
        if (file.exists()) {
            file.delete();
        }
        FileWriter fstream = new FileWriter(path + baseModulePath + "/module.xml");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("<module xmlns=\"urn:jboss:module:1.1\" name=\"" + modulename + "\">\n");
        out.write("    <properties>\n");
        out.write("        <property name=\"jboss.api\" value=\"private\"/>\n");
        out.write("    </properties>\n");

        out.write("    <resources>\n");
        out.write("        <resource-root path=\"" + jarName + "\"/>\n");
        out.write("    </resources>\n");

        out.write("    <dependencies>\n");
        out.write("        <module name=\"sun.jdk\"/>\n");
        out.write("        <module name=\"javax.servlet.api\"/>\n");
        out.write("        <module name=\"org.jboss.as.web\"/>\n");
        out.write("       <module name=\"org.jboss.logging\"/>\n");
        out.write("    </dependencies>\n");
        out.write("</module>");
        out.close();
    }
}