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.yunmel.syncretic.utils.io.IOUtils.java

/**
 * /*from  ww  w  . j  a  v  a  2  s. c  om*/
 * 
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (IOUtils.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:modnlp.capte.AlignerUtils.java

public static void writeAlignment(String sourcename, Vector<Object[]> d) {
    //Output the current alignment as a tab separated file
    String sp = "";

    BufferedWriter pw;
    try {/*ww w  .j  av  a 2  s .  c o  m*/
        pw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sourcename), "UTF-8"));

        //PrintWriter tp = new PrintWriter(new OutputStreamWriter(new FileOutputStream(targetname),"UTF8"));
        String source = "";
        String target = "";
        Object[] stemp;

        for (int i = 0; i < d.size(); i++) {
            stemp = d.get(i);
            source = (String) stemp[0];
            target = (String) stemp[1];
            source = clean(source);
            target = clean(target);
            sp += source + "\t" + target + "\n";

        }

        pw.write(new String(UnicodeUtil.convert(sp.getBytes(), "UTF-8")));
        pw.close();
        //return sp;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:dsfixgui.FileIO.DSFixFileController.java

/**
 *Writes a string to a text file//from  w  w w .ja v  a2 s  .  c o  m
 * 
 * @param filePath
 *  the path of the file to be read, including the filename
 * @param text
 *  the String to be written to the file; can be more than one line.
 * @param overwrite
 *  determines whether the user wants to overwrite the write file if it
 *  already exists. If true, pre-existing file will be overwritten
 * @throws IIOException
 *  if the write file already exists and the user allowed overwriting, but
 *  the file could not be overwritten
 * @throws AccessDeniedException
 *  if the write file already exists but the user didn't allow overwriting
 * @throws IOException
 *  if an error occurs initializing the BufferedWriter
 */
public static void writeTextFile(String filePath, String text, boolean overwrite)
        throws IIOException, IOException, AccessDeniedException {

    //The file to be written
    File writeFile = new File(filePath);
    if (writeFile.exists() && overwrite) {
        //If file exists, try to delete it
        if (!writeFile.delete()) {
            //If file cannot be deleted, throw OIOException
            throw new IIOException("Could not delete pre-existing file: " + filePath);
        }
    } else if (writeFile.exists() && !overwrite) {
        //If file exists but is not allowed to be overwritten, throw AccessDeniedException
        throw new AccessDeniedException(writeFile.getPath());
    }

    //Format each linebreak to be displayed correctly in a text file
    String formattedText = text.replaceAll("\n", String.format("%n"));
    //Initialize BufferedWriter to write string to file
    BufferedWriter fileWriter = new BufferedWriter(new FileWriter(writeFile));
    //Write the file
    Scanner scanner = new Scanner(formattedText);
    while (scanner.hasNextLine()) {
        fileWriter.write(scanner.nextLine());
        fileWriter.newLine();
    }

    fileWriter.close();
}

From source file:com.tencent.wetest.common.util.ReportUtil.java

public static void delRecord(String name) {

    path = WTApplication.getContext().getFilesDir().getPath();

    File f = new File(path + "/wtIndex");
    String fileDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/wetest";
    File tmp = new File(fileDir + "/" + name);
    if (tmp.exists()) {
        tmp.delete();/*from w  w w .j  a  v a  2  s .  com*/
    }
    List<String> content = new ArrayList<String>();

    if (f.exists() && f.isFile()) {
        try {
            BufferedReader indexreader = new BufferedReader(new FileReader(f));
            String br = "";
            while ((br = indexreader.readLine()) != null) {
                if (!br.split("/")[0].equals(name))
                    content.add(br);
            }

            indexreader.close();
            if (content.size() != 0) {
                BufferedWriter indexwriter = new BufferedWriter(new FileWriter(f, false));
                int i = 0;
                for (String temp : content) {

                    if (i == content.size() - 1)
                        indexwriter.write(temp);
                    else
                        indexwriter.write(temp + "\t\n");
                    i++;
                }

                indexwriter.flush();
                indexwriter.close();

            } else {

                f.delete();

            }

        } catch (Exception e) {
            Logger.error("delException:" + e.toString());
            e.printStackTrace();
        }

    }
}

From source file:com.websqrd.catbot.setting.CatbotSettings.java

private static boolean saveXmlRaw(String configFile, String setting) {
    logger.debug("save xml = " + configFile);
    try {/*ww w . j  a  v  a  2 s  .c  o  m*/
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile)));
        bw.write(setting);
        bw.close();
        return true;
    } catch (IOException e) {
        logger.error(configFile + " ? ? ??.", e);
        return false;
    }
}

From source file:data_gen.Data_gen.java

public static void generate_field_map() {
    try {/*  ww  w .j av  a 2  s .  c  o  m*/
        File file = new File(output_dir + "/" + "fieldMap.xml");

        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "\n");
        bw.write("<fieldMap name=\"Default Field Map\" version=\"1.0\">" + "\n");
        bw.write("     <!--" + "\n");
        bw.write("     There are no field maps for the Law Exporter" + "\n");
        bw.write("     -->" + "\n");
        bw.write("</fieldMap>" + "\n");
        bw.close();

    } catch (IOException e) {
        System.out.println("There was an error writing to the xml file." + "\n");
    }

}

From source file:it.evilsocket.dsploit.core.System.java

public static synchronized void errorLog(String tag, String data) {
    String filename = (new File(Environment.getExternalStorageDirectory().toString(), ERROR_LOG_FILENAME))
            .getAbsolutePath();/*from w  w w .j  a  v  a2  s . c  o  m*/

    data = data.trim();

    if (mContext != null && getSettings().getBoolean("PREF_DEBUG_ERROR_LOGGING", false) == true) {
        try {
            FileWriter fWriter = new FileWriter(filename, true);
            BufferedWriter bWriter = new BufferedWriter(fWriter);

            bWriter.write(data);

            bWriter.close();
        } catch (IOException ioe) {
            Log.e(TAG, ioe.toString());
        }
    }

    Log.e(tag, data);
}

From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java

public static void storeCentroids(Configuration configuration, String cenDir, Table<DoubleArray> cenTable,
        int cenVecSize, String name) throws IOException {
    String cFile = cenDir + File.separator + "out" + File.separator + name;
    Path cPath = new Path(cFile);
    LOG.info("centroids path: " + cPath.toString());
    FileSystem fs = FileSystem.get(configuration);
    fs.delete(cPath, true);/* w w w.ja  v a  2  s.  c o  m*/
    FSDataOutputStream out = fs.create(cPath);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
    int linePos = 0;
    int[] idArray = cenTable.getPartitionIDs().toArray(new int[0]);
    IntArrays.quickSort(idArray);
    for (int i = 0; i < idArray.length; i++) {
        Partition<DoubleArray> partition = cenTable.getPartition(idArray[i]);
        for (int j = 0; j < partition.get().size(); j++) {
            linePos = j % cenVecSize;
            if (linePos == (cenVecSize - 1)) {
                bw.write(partition.get().get()[j] + "\n");
            } else if (linePos > 0) {
                // Every row with vectorSize + 1 length,
                // the first one is a count,
                // ignore it in output
                bw.write(partition.get().get()[j] + " ");
            }
        }
    }
    bw.flush();
    bw.close();
}

From source file:com.vmware.bdd.utils.CommonUtil.java

public static void writeFile(File file, String content, boolean append) {
    OutputStream out = null;//w ww. ja v  a  2 s.c om
    BufferedWriter bw = null;
    try {
        out = new FileOutputStream(file, append);
        bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        bw.write(content);
        bw.flush();
    } catch (IOException e) {
        logger.warn("Write file failed : " + e.getMessage());
    } finally {
        if (bw != null && out != null) {
            try {
                bw.close();
                out.close();
            } catch (IOException e) {
                logger.warn("Close file failed : " + e.getMessage());
            }
        }
    }
}

From source file:de.uni_luebeck.inb.knowarc.usecases.invocation.local.LocalUseCaseInvocation.java

public static void persist(File directory) {
    File invocationsFile = new File(directory, LOCAL_INVOCATION_FILE);
    BufferedWriter writer = null;
    try {/* www . j  av  a2  s. c o m*/
        writer = new BufferedWriter(new FileWriter(invocationsFile));
        for (String runId : runIdToTempDir.keySet()) {
            for (String tempDir : runIdToTempDir.get(runId)) {
                writer.write(runId);
                writer.write(" ");
                writer.write(tempDir);
                writer.newLine();
            }
        }
    } catch (IOException e) {
        logger.error(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }
}