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.ibm.bi.dml.runtime.matrix.CleanupMR.java

/**
 * //  w ww  .j av a  2 s . c o  m
 * @param path
 * @param numTasks
 * @throws DMLRuntimeException
 * @throws IOException
 */
private static void writeCleanupTasksToFile(Path path, int numTasks) throws DMLRuntimeException, IOException {
    BufferedWriter br = null;
    try {
        FileSystem fs = FileSystem.get(ConfigurationManager.getCachedJobConf());
        br = new BufferedWriter(new OutputStreamWriter(fs.create(path, true)));

        for (int i = 1; i <= numTasks; i++)
            br.write(String.valueOf("CLEANUP TASK " + i) + "\n");
    } catch (Exception ex) {
        throw new DMLRuntimeException("Error writing cleanup tasks to taskfile " + path.toString(), ex);
    } finally {
        if (br != null)
            br.close();
    }
}

From source file:gamlss.utilities.MatrixFunctions.java

/**
 * Write vector values to CSV file.//  w  w w . ja v a  2 s .c o m
 * @param cmd - path to the file
 * @param v - vector to write
 */
public static void vectorWriteCSV(final String cmd, final RealVector v, boolean append) {
    try {
        // Create file 
        FileWriter fstream = new FileWriter(cmd, append);
        BufferedWriter out = new BufferedWriter(fstream);

        for (int j = 0; j < v.getDimension(); j++) {
            out.write(Double.toString(v.getEntry(j)));
            out.newLine();
        }
        out.close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();/*from   ww  w. j  a v  a2s. c o m*/
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

/**
 * Creates a text file containing all full paths to the images in the directory and its subdirectories.
 *
 * @param imageDirectory the directories where the images can be found.
 * @param outputFile     the text file to be written (to)
 * @param append         set to false to overwrite.
 * @return the number of images found / lines written the output file.
 * @throws IOException//from   ww  w  .  j a  v  a 2 s. com
 */
public static int createImagefileList(File imageDirectory, File outputFile, boolean append) throws IOException {
    if (!imageDirectory.isDirectory())
        return -1;
    int result = 0;
    Collection<File> files = org.apache.commons.io.FileUtils.listFiles(imageDirectory,
            new String[] { "jpg", "png", "PNG", "JPG" }, true);
    BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile, append));
    for (File f : files) {
        bw.write(f.getAbsolutePath() + "\n");
        result++;
    }
    bw.close();
    return result;
}

From source file:com.hp.test.framework.Reporting.Utlis.java

public static void replaceMainTable(Boolean onlyMainTable, File f) {

    String content = "";
    String content1 = "";
    String replace = "";
    String imagePath = "../../../../../../../";
    if (f.getName().contentEquals("index.html")) {
        imagePath = "";
    }//from w w w  .  jav  a2s .co m

    if (f.getName().contentEquals("ConsolidatedPage.html")) {
        imagePath = "../";
    }

    if (f.getName().contentEquals("index.html")) {
        imagePath = "";
    }

    if (f.getName().contentEquals("CurrentRun.html")) {
        imagePath = "../../";
    }

    replace = "<tr id=\"header\" >" + "\n";
    replace = replace + "<td id=\"logo\"><img src=\"" + imagePath
            + "HTML_Design_Files/IMG/Framework_Logo.jpg\" alt=\"Logo\" height=\"70\" width=\"140\" /> <br/><i style=\"float:left;padding-left:20px;font-size:12px\">Reflections of Visionary Minds</i></td>";
    replace = replace + " <td id=\"headertext\">";
    replace = replace + "Intelligent Solutions Test Framework";
    replace = replace + "<div style=\"padding-right:20px;float:right\"><img src=\"" + imagePath
            + "HTML_Design_Files/IMG/hp.png\" height=\"70\" width=\"140\" /> </i></div>                </td>";
    replace = replace + " </tr>";

    try {
        BufferedReader in = new BufferedReader(new FileReader(f));

        String str;
        Boolean found = false;
        boolean readingdone = false;
        while ((str = in.readLine()) != null) {

            content1 += str;
            if (!onlyMainTable) {
                if (str.contains("<td id=\"content\">")) {

                    content1 += " <img src=\"HTML_Design_Files\\\\IMG\\reports.jpg\"  alt=\"BackGround\" height=\"500\" width=\"500\" /> ";
                    in.readLine();
                    continue;
                }
            }
            if (str.contains("<table id=\"mainTable\">")) {
                found = true;

            }

            if (found) {
                while ((str = in.readLine()) != null) {

                    if (str.contains("</tr>")) {
                        found = false;
                        readingdone = true;
                        content1 += replace;
                        break;

                    }
                }

            }

        }
        in.close();
        BufferedWriter out = new BufferedWriter(new FileWriter(f));

        out.write(content1);
        out.close();

    } catch (IOException e) {
    }
}

From source file:com.github.DroidPHP.ServerUtils.java

final private static void restoreConfiguration(String fileName) {

    File isConf = new File(getHttpDirectory() + "/conf/" + fileName);
    if (!isConf.exists()) {

        try {/*w w w  . java2s  .com*/

            String mString;

            java.io.InputStream mStream = mContext.getAssets().open(fileName, AssetManager.ACCESS_BUFFER);

            java.io.BufferedWriter outputStream = new java.io.BufferedWriter(
                    new java.io.FileWriter(getHttpDirectory() + "/tmp/" + fileName));

            int c;
            while ((c = mStream.read()) != -1) {
                outputStream.write(c);
            }
            outputStream.close();
            mStream.close();

            mString = org.apache.commons.io.FileUtils
                    .readFileToString(new File(getHttpDirectory() + "/tmp/" + fileName), "UTF-8");

            mString = mString.replace("%app_dir%", getAppDirectory());
            mString = mString.replace("%http_dir%", getHttpDirectory());
            mString = mString.replace("%port%", serverPort);
            org.apache.commons.io.FileUtils
                    .writeStringToFile(new File(getHttpDirectory() + "/conf/" + fileName), mString, "UTF-8");
        } catch (java.lang.Exception e) {
            Log.e(TAG, "Unable to copy " + fileName + " from assets", e);

        }
    }

}

From source file:com.thoughtmetric.tl.TLLib.java

private static TagNode TagNodeFromURLHelper(InputStream is, String fullTag, Handler handler, Context context,
        HtmlCleaner cleaner) throws IOException {
    SharedPreferences settings = context.getSharedPreferences(Settings.SETTINGS_FILE_NAME, 0);
    boolean disableSmartParsing = settings.getBoolean(Settings.DISABLE_SMART_PARSING, false);
    if (fullTag != null && !disableSmartParsing) {
        FileOutputStream fos = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        TagParser.extractTagToFile(fullTag, is, bw);
        bw.flush();/* ww  w. j  a  va  2  s.  c  o m*/
        bw.close();
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);

        return cleaner.clean(context.openFileInput(TEMP_FILE_NAME));
    } else {
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);
        return cleaner.clean(is);
    }
}

From source file:Main.java

public static void saveUtfFileWithBOM(File file, String content) throws IOException {
    BufferedWriter bw = null;
    OutputStreamWriter osw = null;

    FileOutputStream fos = new FileOutputStream(file);
    try {//from  w  w  w  . j ava  2  s  . co  m
        // write UTF8 BOM mark if file is empty
        if (file.length() < 1) {
            final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
            fos.write(bom);
        }

        osw = new OutputStreamWriter(fos, "UTF-8");
        bw = new BufferedWriter(osw);
        if (content != null) {
            bw.write(content);
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        try {
            bw.close();
            fos.close();
        } catch (Exception ex) {
        }
    }
}

From source file:canreg.client.analysis.Tools.java

public static void exportChartAsCSV(JFreeChart jFreeChart, File file) throws IOException {
    BufferedWriter bos = new BufferedWriter(new FileWriter(file));
    bos.append(getChartData(jFreeChart, ",", true));
    bos.flush();//from www.  j  a  va  2  s  . com
    bos.close();
}

From source file:atg.tools.dynunit.util.ComponentUtil.java

private static void writeComponentPropertiesToFile(final File output, final String canonicalClassName,
        final Properties properties) throws IOException {
    logger.entry(output, canonicalClassName, properties);
    BufferedWriter out = null;
    try {/*w  ww.j  av a2 s  . c  o m*/
        out = new BufferedWriter(new FileWriter(output));
        if (StringUtils.isNotEmpty(canonicalClassName)) {
            out.write("$class=");
            out.write(canonicalClassName);
            out.newLine();
        }
        properties.store(out, canonicalClassName);
    } finally {
        if (out != null) {
            out.close();
        }
        logger.exit();
    }
}