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.beginner.core.utils.Tools.java

/**
 * ?txt?//from w  w  w .ja  va 2s.c  om
 * @param fileP    
 * @param content    
 * @since          1.0.0
 */
public static void writeFile(String fileP, String content) {
    String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; //
    filePath = (filePath.trim() + fileP.trim()).substring(6).trim();
    if (filePath.indexOf(":") != 1) {
        filePath = File.separator + filePath;
    }
    try {
        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
        BufferedWriter writer = new BufferedWriter(write);
        writer.write(content);
        writer.close();

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

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

/**
 * Returns a Gravatar ID associated with the provided name
 * // w w  w . ja  v  a 2s  .  c o  m
 * @param name
 * @return the gravatar ID associated with the name
 */
public static String getGravatarID(final String name) {
    String id = "";
    try {
        final File gravatars = ensure_directory(ROOT_DIR);
        final File gravatar_id = new File(gravatars, name + ".id");

        if (gravatar_id.isFile()) {
            final FileReader fr = new FileReader(gravatar_id);
            final BufferedReader in = new BufferedReader(fr);
            id = in.readLine();
            in.close();
        } else {
            id = getGravatarIdFromGithub(name);
            if (!id.equals("")) {
                final FileWriter fw = new FileWriter(gravatar_id);
                final BufferedWriter bw = new BufferedWriter(fw);
                bw.write(id);
                bw.flush();
                bw.close();
            }
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return id;
}

From source file:com.adito.activedirectory.ActiveDirectoryPropertyManager.java

private static void close(BufferedWriter writer) {
    try {//w w  w.j a  v  a 2  s  .  c  o  m
        if (writer != null) {
            writer.close();
        }
    } catch (IOException e) {
        // ignore
    }
}

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

public final static void writeAndBackup(File file, String content) {
    try {//from www  . j  a  v a2s  .com
        DateFormat backupDF = new SimpleDateFormat("ddMMyy_hhmmss");

        File backupDirectory = checkBackupDirectory(file);

        File original = new File(file.getAbsolutePath());

        File backup = new File(backupDirectory, original.getName() + "." + backupDF.format(new Date()));

        if (log.isDebugEnabled()) {
            log.debug("Backup file is " + backup);
        }

        original.renameTo(backup);

        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:com.dahl.brendan.wordsearch.view.IOService.java

private static void writeFile(Context context, File file, boolean overwrite) {
    if (!file.exists() || overwrite) {
        Cursor cursor = context.getContentResolver().query(Word.CONTENT_URI, new String[] { Word.WORD }, null,
                null, null);/*  ww  w.ja  v  a 2  s .c  om*/
        JSONArray array = new JSONArray();
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                array.put(cursor.getString(0));
                cursor.moveToNext();
            }
        }
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(array.toString());
            out.close();
        } catch (IOException e) {
            Log.e(LOGTAG, "write failed", e);
        }
    }
}

From source file:edu.cmu.cs.lti.ark.fn.data.prep.ParsePreparation.java

/**
* Writes the given sentences to the given file
*
* @param outputFile the file to write to
* @param sentences the sentences to write
*///from w w  w  .ja  v  a  2  s  . c o  m
public static void writeSentencesToFile(String outputFile, List<String> sentences) {
    try {
        final BufferedWriter bWriter = new BufferedWriter(new FileWriter(outputFile));
        for (String sentence : sentences) {
            bWriter.write(sentence.trim() + "\n");
        }
        bWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Write the provided String, line by line, in the provided OutputStream.
 *///w w  w .  ja  va2  s  .c om
private static void writeStream(String toWrite, OutputStream out) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
    BufferedReader reader = new BufferedReader(new StringReader(toWrite));
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        writer.write(line);
    }
    reader.close();
    writer.close();
}

From source file:eu.liveandgov.ar.utilities.OS_Utils.java

/**
 *         Save a string into a file in SD 
 *//*from  w w w  . j  a  v  a 2s  .  co  m*/
public static boolean write2File(String filename, String string) {
    try {

        FileWriter fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(string);
        out.close();
        fstream.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:de.dakror.scpuller.SCPuller.java

public static void saveDownloadedSongs() {
    File f = new File(System.getProperty("user.home") + "/.dakror/SCPuller/downloaded.txt");
    try {// w ww.ja v  a  2  s.  c o  m
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        for (int i : downloadedSongs)
            bw.write(i + "\r\n");

        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;/*from  w  w  w.  j  ava  2  s. c o  m*/
    file = new File(path);
    if (!file.exists()) {
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}