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

public static void writeFile(String filepath, InputStream is) throws IOException {
    File file = new File(filepath);
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line;/*from  w w w.j  a  v a  2 s.c om*/
    while ((line = br.readLine()) != null) {
        bw.write(line + "\n");
    }

    bw.close();
    br.close();
}

From source file:com.dragovorn.dragonbot.api.bot.file.FileManager.java

public static void setDirectory(String directory) {
    File file = new File(directory, "Dragon Bot");

    if (FileManager.directory.exists() && !file.exists()) {
        if (file.mkdirs()) {
            try {
                FileUtils.copyDirectory(FileManager.directory, file);
            } catch (IOException exception) {
                exception.printStackTrace();
            }/*w  ww .j a v a2  s  .  co m*/
        } else {
            throw new RuntimeException("Unable to create file: " + file.getName()); // FIXME: 11/23/16 make custom exception
        }

        try {
            FileUtils.deleteDirectory(FileManager.directory);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    FileManager.directory = file;

    File path = new File(FileManager.dir, "path");

    try {
        FileWriter fileWriter = new FileWriter(path);

        BufferedWriter writer = new BufferedWriter(fileWriter);

        writer.write(directory);

        writer.close();
        fileWriter.close();
    } catch (IOException exception) {
        exception.printStackTrace();
    }

    reloadFiles();
}

From source file:com.cloudera.sqoop.manager.MySQLUtils.java

/**
 * Writes the user's password to a tmp file with 0600 permissions.
 * @return the filename used.//from  w w w  .  j av  a2  s .  com
 */
public static String writePasswordFile(Configuration conf) throws IOException {
    // Create the temp file to hold the user's password.
    String tmpDir = conf.get(ConfigurationConstants.PROP_JOB_LOCAL_DIRECTORY, "/tmp/");
    File tempFile = File.createTempFile("mysql-cnf", ".cnf", new File(tmpDir));

    // Make the password file only private readable.
    DirectImportUtils.setFilePermissions(tempFile, "0600");

    // If we're here, the password file is believed to be ours alone.  The
    // inability to set chmod 0600 inside Java is troublesome. We have to
    // trust that the external 'chmod' program in the path does the right
    // thing, and returns the correct exit status. But given our inability to
    // re-read the permissions associated with a file, we'll have to make do
    // with this.
    String password = conf.get(PASSWORD_KEY);
    BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile)));
    w.write("[client]\n");
    w.write("password=" + password + "\n");
    w.close();

    return tempFile.toString();
}

From source file:Main.java

public static String UpdateScore(String userName, int br) {

    String retStr = "";

    try {//from www . j a v a 2  s  . c  om
        URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/AzurirajHighScore.php");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        Log.e("http", "por1");

        JSONObject data = new JSONObject();

        data.put("username", userName);
        data.put("broj", br);

        Log.e("http", "por3");

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString());
        String query = builder.build().getEncodedQuery();

        Log.e("http", "por4");

        OutputStream os = conn.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        bw.write(query);
        Log.e("http", "por5");
        bw.flush();
        bw.close();
        os.close();
        int responseCode = conn.getResponseCode();

        Log.e("http", String.valueOf(responseCode));
        if (responseCode == HttpURLConnection.HTTP_OK) {
            retStr = inputStreamToString(conn.getInputStream());
        } else
            retStr = String.valueOf("Error: " + responseCode);

        Log.e("http", retStr);

    } catch (Exception e) {
        Log.e("http", "greska");
    }
    return retStr;
}

From source file:Main.java

public static String UdatePlayerBT(String userName, String device) {

    String retStr = "";

    try {//from w  w w .  ja v  a2s.c  om
        URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/PostaviBTdevice.php");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        Log.e("http", "por1");

        JSONObject data = new JSONObject();

        data.put("username", userName);
        data.put("bt_device", device);

        Log.e("http", "por3");

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString());
        String query = builder.build().getEncodedQuery();

        Log.e("http", "por4");

        OutputStream os = conn.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        bw.write(query);
        Log.e("http", "por5");
        bw.flush();
        bw.close();
        os.close();
        int responseCode = conn.getResponseCode();

        Log.e("http", String.valueOf(responseCode));
        if (responseCode == HttpURLConnection.HTTP_OK) {
            retStr = inputStreamToString(conn.getInputStream());
        } else
            retStr = String.valueOf("Error: " + responseCode);

        Log.e("http", retStr);

    } catch (Exception e) {
        Log.e("http", "greska");
    }
    return retStr;
}

From source file:co.foxdev.foxbot.utils.Utils.java

public static boolean addCustomCommand(String channel, String command, String text) {
    String filePath = "data/custcmds/" + channel.substring(1);
    File path = new File(filePath);

    try {//from   www  . java2  s  .  c  o m
        if (!path.exists() && !path.mkdirs()) {
            foxbot.getLogger().warn("Error occurred while creating custom command folders!");
        }

        File file = new File(filePath + "/" + command);

        if (file.exists() && !file.delete()) {
            foxbot.getLogger().warn(
                    String.format("Error occurred while deleting command '%s' for %s!", command, channel));
        }

        if (text.isEmpty() || text.equalsIgnoreCase("delete")) {
            if (file.delete()) {
                foxbot.getLogger().warn(String.format("Command '%s' deleted for %s!", command, channel));
            }
            return false;
        }

        FileWriter fw = new FileWriter(filePath + "/" + command);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(text);
        bw.flush();
        bw.close();
        fw.flush();
        fw.close();
        foxbot.getLogger()
                .warn(String.format("Command '%s' set for %s at %s", command, channel, file.getAbsolutePath()));
    } catch (IOException ex) {
        foxbot.getLogger().error("Error occurred while adding custom command", ex);
    }
    return true;
}

From source file:be.ac.ua.comp.scarletnebula.core.KeyManager.java

/**
 * Takes a keyname, an actual key and a provider name (for which this key
 * works) and then stores the key on disk.
 * /*w  w w . j ava  2 s . c o m*/
 * @param providerName
 * @param keyname
 * @param keystring
 */
static void addKey(final String providerName, final String keyname, final String keystring) {
    if (assureDirectory(providerName) == null) {
        return;
    }

    // Now store the key to file
    BufferedWriter out;
    try {
        out = new BufferedWriter(new FileWriter(getKeyFilename(providerName, keyname)));
        out.write(keystring);
        out.close();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.opensymphony.util.FileUtils.java

public final static void write(File file, String content) {
    try {//from  w  w w  .j  a  v  a2s .  c o  m
        BufferedWriter out = new BufferedWriter(new FileWriter(file));

        out.write(content);
        out.close();
    } catch (FileNotFoundException e) {
        log.warn("File not found", e);
    } catch (IOException e) {
        log.error(e);
    }
}

From source file:forge.error.BugReporter.java

public static void saveToFile(final String text) {
    File f;/* w  ww. j ava2s .co  m*/
    final long curTime = System.currentTimeMillis();
    for (int i = 0;; i++) {
        final String name = String.format("%TF-%02d.txt", curTime, i);
        f = new File(name);
        if (!f.exists()) {
            break;
        }
    }

    f = GuiBase.getInterface().getSaveFile(f);

    try {
        final BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write(text);
        bw.close();
    } catch (final IOException ex) {
        SOptionPane.showMessageDialog("There was an error during saving. Sorry!\n" + ex, "Error saving file",
                SOptionPane.ERROR_ICON);
    }
}

From source file:org.esupportail.twitter.services.OAuthTwitterApplicationOnlyService.java

private static boolean writeRequest(HttpURLConnection connection, String textBody) {
    try {//  w  w w  . j  av  a 2 s  .  co  m
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        wr.write(textBody);
        wr.flush();
        wr.close();

        return true;
    } catch (IOException e) {
        return false;
    }
}