Example usage for java.io BufferedWriter write

List of usage examples for java.io BufferedWriter write

Introduction

In this page you can find the example usage for java.io BufferedWriter write.

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

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

public static void writeFile(File file, String content, boolean append) {
    OutputStream out = null;//from  ww  w . j ava  2 s.co m
    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:bridge.toolkit.commands.S1000DConverter.java

/**
 * Write the string in the file on disk/*from   w ww .j  av  a 2s. co m*/
 * 
 * @param file
 * @param contenuto
 * @throws Exception
 */
public static void writeOnDisk(File file, String contenuto) throws Exception {
    try {
        BufferedWriter outWriter = null;

        outWriter = new BufferedWriter(new java.io.FileWriter(file));
        outWriter.write(contenuto);
        outWriter.flush();
        outWriter.close();
    } catch (Exception ex) {
        throw ex;
    }
}

From source file:com.oocl.euc.ita.core.utils.FileUtils.java

public static void writefile(String path, String content, boolean append) {
    BufferedWriter bw;
    File targetFile;//from   w w w . j  a v  a2 s .  c  o  m
    try {
        targetFile = new File(path);
        if (targetFile.exists() == false) {
            targetFile.createNewFile();
        }
        FileWriter fw = new FileWriter(targetFile, append);
        bw = new BufferedWriter(fw);
        bw.write(content + "\n");
        bw.flush();
        bw.close();
    } catch (Exception d) {
        System.out.println(d.getMessage());
    }
}

From source file:ar.com.tadp.xml.rinzo.core.utils.FileUtils.java

/**
 * Guarda un documento localmente en la cache
 *//*from  w w  w.  j  a va  2  s.  c o m*/
public static void saveFile(String inputFileName, File outputFile) {
    InputStream openStream = null;
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
        File inputFile = new File(inputFileName);
        if (!inputFile.exists()) {
            openStream = new URL(inputFileName).openStream();
            InputStreamReader is = new InputStreamReader(openStream);
            String encoding = is.getEncoding();
            reader = new BufferedReader(is);
            writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(outputFile, false), encoding));

            String line = reader.readLine();
            while (line != null) {
                writer.write(line);
                writer.newLine();
                writer.flush();
                line = reader.readLine();
            }
        }
    } catch (Exception exception) {
        throw new RuntimeException("Error trying to save \'" + inputFileName + "\' in the cache.", exception);
    } finally {
        try {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
            if (reader != null) {
                reader.close();
            }
            if (openStream != null) {
                openStream.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(
                    "Error trying to close files while saving \'" + inputFileName + "\' in the cache.", e);
        }
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

public static void appendTextToFile(String filePath, String text) throws Exception {

    FileWriter fileWriter = null;
    BufferedWriter bufferWritter = null;

    try {/* w ww .j av a 2 s .  c  o m*/
        fileWriter = new FileWriter(filePath, true);
        bufferWritter = new BufferedWriter(fileWriter);

        bufferWritter.write(text);

    } catch (IOException error) {
        throw error;
    } finally {

        if (bufferWritter != null) {
            bufferWritter.close();
        }

        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}

From source file:com.hp.test.framework.htmparse.HtmlParse.java

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

    try {/* ww  w.ja  va2 s  .c o  m*/
        String[] filename = f.getName().split("/.");
        String absolutePath = f.getAbsolutePath();
        String filePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
        BufferedReader in = new BufferedReader(new FileReader(f));
        String Filepathof_newFile = filePath + "/" + filename[0] + "temp.html";
        ReportingProperties reportingprop = new ReportingProperties();
        String customizedText = reportingprop.getProperty("CustomizedText");
        String CustomizedTitle = reportingprop.getProperty("CustomizedTitle");
        reportingprop = null;

        BufferedWriter out = new BufferedWriter(new FileWriter(Filepathof_newFile));
        //    BufferedWriter out = new BufferedWriter(new FileWriter(filePath + "/" + filename[0] ));

        String str;
        while ((str = in.readLine()) != null) {

            if (str.contains(
                    "<div class=\"chartStyle\" style=\"text-align: left;margin-left: 30px;float: left;width: 60%;\"> ")) {
                //   out.write("</div> <div     class=\"chartStyle\" style=\"text-align: left;margin-left: 30px;float: left;width: 30%;\"> ");
                out.write(str);
                out.write(in.readLine());
                out.write(in.readLine());
                out.write(in.readLine());
                //  out.write("\n");
                out.write(
                        "<div class=\"chartStyle\" style=\"text-align: left;margin-left: 5px;float: left;width: 95%;\">");
                out.write("\n");
                out.write("<div id=\"chartdiv\" style=\"width: 1300px; height: 350px;\"></div>");
                out.write("\n");
                out.write("</div>");
                continue;

            }

            if (str.contains("No Video Recording Available"))
                continue;
            if (str.contains("Run Description")) {
                str = str.replace("Run Description", CustomizedTitle);
                str = str.replace("Here you can give description about the current Run", customizedText);
                out.write(str);
                out.write("\n");
                continue;
            }
            //                if(str.contains("Here you can give description about the current Run"))
            //                {
            //                    str=str.replace("Here you can give description about the current Run", customizedText);
            //                   out.write(str);
            //                   out.write("\n");
            //                   continue;
            //                }
            if (!str.contains("Current Run Reports")) {
                out.write(str);
                out.write("\n");

            } else {
                out.write(str);
                out.write("\n");

                out.write(" <link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\">");
                out.write("\n");
                out.write("<script src=\"amcharts.js\" type=\"text/javascript\"></script>");
                out.write("\n");
                out.write("<script src=\"serial.js\" type=\"text/javascript\"></script>");
                out.write("\n");
                out.write("<script src=\"3dChart.js\" type=\"text/javascript\"></script>");
                out.write("\n");
            }

        }
        in.close();
        out.close();
        File renameFile = new File(Filepathof_newFile);

        FileUtils.copyFile(renameFile, f);
        System.out.println("Replacing Counts Feature wise is Completed");
    } catch (IOException e) {
        System.out.println("Exception replacing Counts Feature wise" + e.getLocalizedMessage());
    }
}

From source file:com.gargoylesoftware.htmlunit.source.JQueryExtractor.java

/**
 * Transforms the raw expectation, to the needed one by HtmlUnit.
 * This methods puts only the main line of the test output, without the details.
 *
 * @param input the raw file of real browser, with header and footer removed
 * @param output the expectation//ww w .  j  a  v a2 s  .  c om
 * @throws IOException if an error occurs
 */
public static void extractExpectations(final File input, final File output) throws IOException {
    final BufferedReader reader = new BufferedReader(new FileReader(input));
    final BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    int testNumber = 1;
    String line;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        final int endPos = line.indexOf("Rerun");
        // the test number is at least for 1.11.3 no longer part of the output
        // instead a ordered list is used by qunit
        // if (line.startsWith("" + testNumber + '.') && endPos > -1) {
        if (endPos > -1) {
            line = line.substring(0, endPos);
            writer.write(line + "\n");
            testNumber++;
        } else if (line.endsWith("Rerun")) {
            if (line.indexOf("" + testNumber + '.', 4) != -1) {
                System.out.println("Incorrect line for test# " + testNumber + ", please correct it manually");
                break;
            }
            line = "" + testNumber + '.' + ' ' + line.substring(0, line.length() - 5);
            writer.write(line + "\n");
            testNumber++;
        }
    }
    System.out.println("Last output #" + (testNumber - 1));
    reader.close();
    writer.close();
}

From source file:core.Utility.java

public static void generateCategoryDataCSV(String word) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(new File("output files/" + word + ".csv")));

    int count = 0;
    ArrayList<String> miscList = new ArrayList();

    // calculate miscelleneous list
    for (Entity e : Global.entities) {
        if (e.categories.isEmpty()) {
            miscList.add(e.word);/*from w ww  . j a  v a 2 s. c o  m*/
        }
    }

    count = miscList.size();

    // calculate maximum count
    for (Category c : Global.categories) {
        int itemCount = c._relatedWords.size();
        count = itemCount > count ? itemCount : count;
        writer.write(c._name + ",,");
    }
    writer.write("Miscellineous");

    writer.newLine();

    for (int c = 0; c < count; c++) {
        for (Category cat : Global.categories) {
            String oldWord = (c <= (cat._OriginalList.size() - 1)) ? cat._OriginalList.get(c)._name : "";
            String newWord = (c <= (cat._NewList.size() - 1)) ? cat._NewList.get(c)._name : "";

            // write
            writer.write(oldWord + "," + newWord + ",");
        }

        String miscWord = (c <= (miscList.size() - 1)) ? miscList.get(c) : "";

        // write  misc word
        writer.write(miscWord + ",");
        writer.newLine();
    }
    //        for (int i = -1; i < Global.categories.size(); i++) {
    //            if (i > -1) {
    //                writer.newLine();
    //
    //                Category c = Global.categories.get(i);
    //
    //                for (Entity e : Global.entities) {
    //                    writer.write((e.categories.contains(c._name) ? "Yes" : "No") + ",");
    //                }
    //
    //                writer.write(c._name);
    //            } else {
    //                for (Entity e : Global.entities) {
    //                    writer.write(e.word + ",");
    //                }
    //
    //                writer.write("CATEGORY");
    //            }
    //        }
    //
    //        writer.newLine();
    //        for (Entity e : Global.entities) {
    //            writer.write((e.categories.isEmpty() ? "Yes" : "No") + ",");
    //        }
    //        writer.write("Miscellaneous");

    writer.close();
}

From source file:com.yunshan.cloudstack.vmware.util.VmwareHelper.java

public static byte[] composeDiskInfo(List<Ternary<String, String, String>> diskInfo, int disksInChain,
        boolean includeBase) throws IOException {

    BufferedWriter out = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//from  www. j  av  a 2  s .  c om
        out = new BufferedWriter(new OutputStreamWriter(bos));

        out.write("disksInChain=" + disksInChain);
        out.newLine();

        out.write("disksInBackup=" + diskInfo.size());
        out.newLine();

        out.write("baseDiskIncluded=" + includeBase);
        out.newLine();

        int seq = disksInChain - 1;
        for (Ternary<String, String, String> item : diskInfo) {
            out.write(String.format("disk%d.fileName=%s", seq, item.first()));
            out.newLine();

            out.write(String.format("disk%d.baseFileName=%s", seq, item.second()));
            out.newLine();

            if (item.third() != null) {
                out.write(String.format("disk%d.parentFileName=%s", seq, item.third()));
                out.newLine();
            }
            seq--;
        }

        out.newLine();
    } finally {
        if (out != null)
            out.close();
    }

    return bos.toByteArray();
}

From source file:com.smart.common.officeFile.CSVUtils.java

/**
 *
 * @param exportData ?/*from w  w  w  .  j a  v  a  2 s .  c  om*/
 * @param rowMapper ??
 * @param outPutPath 
 * @param filename ??
 * @return
 */
public static File createCSVFile(List exportData, LinkedHashMap rowMapper, String outPutPath, String filename) {

    File csvFile = null;
    BufferedWriter csvFileOutputStream = null;
    try {
        csvFile = new File(outPutPath + filename + ".csv");
        // csvFile.getParentFile().mkdir();
        File parent = csvFile.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        csvFile.createNewFile();

        // GB2312?","
        csvFileOutputStream = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);
        // 
        for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) {
            java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
            csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\"");
            if (propertyIterator.hasNext()) {
                csvFileOutputStream.write(",");
            }
        }
        csvFileOutputStream.newLine();

        // 
        for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
            LinkedHashMap row = (LinkedHashMap) iterator.next();
            System.out.println(row);

            for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
                java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                // System.out.println(BeanUtils.getProperty(row, propertyEntry.getKey().toString()));
                csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\"");
                if (propertyIterator.hasNext()) {
                    csvFileOutputStream.write(",");
                }
            }

            //                for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
            //                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
            //                    System.out.println(BeanUtils.getProperty(row, propertyEntry.getKey().toString()));
            //                    csvFileOutputStream.write("\""
            //                            + BeanUtils.getProperty(row, propertyEntry.getKey().toString()) + "\"");
            //                    if (propertyIterator.hasNext()) {
            //                        csvFileOutputStream.write(",");
            //                    }
            //                }
            if (iterator.hasNext()) {
                csvFileOutputStream.newLine();
            }
        }
        csvFileOutputStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            csvFileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return csvFile;
}