Example usage for java.io BufferedWriter append

List of usage examples for java.io BufferedWriter append

Introduction

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

Prototype

public Writer append(CharSequence csq) throws IOException 

Source Link

Document

Appends the specified character sequence to this writer.

Usage

From source file:org.addhen.smssync.util.Util.java

/**
 * For debugging purposes. Append content of a string to a file
 * /* www .  j  ava  2  s  . c  o  m*/
 * @param text
 */
public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory(), "smssync.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        // BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.append(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:eu.annocultor.common.Utils.java

public static void saveStringToFile(String string, String fileName) throws FileNotFoundException, IOException {
    BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
    out.append(string);
    out.close();//  w  w w.j a va 2s .  c  om
}

From source file:playground.jbischoff.taxi.berlin.supply.TaxiStatusDataAnalyser.java

private static void writeIdleVehiclesByZoneAndStatus(Matrices statusMatrices, String idleVehicles, String start,
        String end) throws ParseException, IOException {

    Set<String> allZones = new HashSet<String>();
    for (Matrix matrix : statusMatrices.getMatrices().values()) {
        allZones.addAll(matrix.getFromLocations().keySet());
    }//from   w w  w  .  j  av a 2s  .  c  om

    BufferedWriter writer = IOUtils.getBufferedWriter(idleVehicles);
    Date currentTime = STATUS_DATE_FORMAT.parse(start);
    Date endTime = STATUS_DATE_FORMAT.parse(end);

    //header
    writer.append("zone");
    while (!currentTime.equals(endTime)) {

        if (currentTime.getMinutes() == 00) {
            writer.append("\t" + STATUS_DATE_FORMAT.format(currentTime));

        }
        currentTime = getNextTime(currentTime);

    }

    for (String zone : allZones) {
        currentTime = STATUS_DATE_FORMAT.parse(start);
        endTime = STATUS_DATE_FORMAT.parse(end);
        double hourlyVehicles = 0;
        writer.newLine();
        writer.append(zone);
        while (!currentTime.equals(endTime)) {

            Matrix m = statusMatrices.getMatrix(STATUS_DATE_FORMAT.format(currentTime));
            if (m != null) {
                if (m.getFromLocations().containsKey(zone)) {
                    for (Entry entry : m.getFromLocEntries(zone)) {
                        switch (entry.getToLocation()) {
                        case "65":
                        case "70":
                        case "80":
                        case "83":
                        case "85": {
                            hourlyVehicles += entry.getValue();
                            break;
                        }
                        default:
                            break;

                        }
                    }
                }
            }
            if (zone.equals("1011401")) {
                System.out.println(STATUS_DATE_FORMAT.format(currentTime) + "\t" + hourlyVehicles);
            }
            if (currentTime.getMinutes() == 55) {
                writer.append("\t" + hourlyVehicles / 12.0);
                hourlyVehicles = 0;

            }

            currentTime = getNextTime(currentTime);
        }

    }
    writer.flush();
    writer.close();
}

From source file:de.unibi.techfak.bibiserv.util.codegen.Main.java

/**
 * Copy a text based file from in to out and replace TEMPLATE_ID with 'id'
 * and TEMPLATE_NAME with 'name'/*from   w w  w .  j av  a  2 s.c  o  m*/
 *
 *
 * @param in
 * @param out
 * @param id
 * @param name
 * @throws IOException
 */
public static void CopyAndReplace(InputStream in, OutputStream out, String id, String name, String version)
        throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));

    String line;

    while ((line = br.readLine()) != null) {
        bw.append(line.replace("TEMPLATE_ID", id).replace("TEMPLATE_NAME", name).replace("TEMPLATE_VERSION",
                version));
        bw.newLine();
    }
    br.close();
    bw.close();

}

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();//  w ww.  jav  a 2 s .c  om
    bos.close();
}

From source file:com.hazelcast.simulator.utils.FileUtils.java

public static void appendText(String text, File file) {
    if (text == null) {
        throw new NullPointerException("Text can't be null");
    }/* w w w  .java2 s.  c  om*/
    if (file == null) {
        throw new NullPointerException("File can't be null");
    }

    FileOutputStream stream = null;
    OutputStreamWriter streamWriter = null;
    BufferedWriter writer = null;
    try {
        stream = new FileOutputStream(file, true);
        streamWriter = new OutputStreamWriter(stream);
        writer = new BufferedWriter(streamWriter);
        writer.append(text);
        writer.close();
    } catch (IOException e) {
        throw new FileUtilsException("Could not append text", e);
    } finally {
        closeQuietly(writer);
        closeQuietly(streamWriter);
        closeQuietly(stream);
    }
}

From source file:org.opentides.util.FileUtil.java

/**
 * Helper class to read certain file and return contents as string. The file
 * must be located relative to classpath.
 * /*from w  w  w . j av  a2s  .c o  m*/
 * @param filename
 * @return
 */
private static String backupFile(BufferedReader reader, BufferedWriter writer, String filename) {
    try {
        String line = null;
        StringBuffer ret = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            writer.append(line + "\n");
        }
        return ret.toString();
    } catch (NullPointerException npe) {
        String msg = "Failed to copy file for backup [" + filename + "].";
        _log.error(msg, npe);
        throw new InvalidImplementationException(msg, npe);
    } catch (IOException ioe) {
        String msg = "Cannot access file for backup [" + filename + "].";
        _log.error(ioe, ioe);
        throw new InvalidImplementationException(msg, ioe);
    } finally {
        try {
            reader.close();
            writer.close();
        } catch (IOException e) {
            // ignore 
        }
    }
}

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

public static LinkedList<String> generateRChart(Collection<CancerCasesCount> casesCounts, String fileName,
        String header, FileTypes fileType, ChartType chartType, boolean includeOther, Double restCount,
        String rpath, boolean sortByCount, String xlab) {
    LinkedList<String> generatedFiles = new LinkedList<String>();

    RFileBuilder rff = new RFileBuilder();

    File script = new File(Globals.R_SCRIPTS_PATH + "/makeSureGgplot2IsInstalled.r");
    rff.appendHeader(script.getAbsolutePath());

    rff.appendFileTypePart(fileType, fileName);

    generatedFiles.add(fileName);//  w w w  .j ava2s.c  om

    rff.appendData(casesCounts, restCount, includeOther);

    int numberOfCategories = casesCounts.size();
    if (includeOther) {
        numberOfCategories += 1;
    }
    if (sortByCount) {
        rff.appendSort(chartType, numberOfCategories, includeOther, restCount);
    }

    rff.appendPlots(chartType, header, xlab);
    rff.appendWriteOut();

    System.out.println(rff.getScript());

    try {
        File tempFile = File.createTempFile("script", ".r");
        // generatedFiles.add(tempFile.getPath());
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(tempFile), "UTF8"));
        writer.append(rff.getScript());
        writer.close();
        Tools.callR(tempFile.getAbsolutePath(), rpath, fileName + "-report.txt");
    } catch (TableErrorException ex) {
        Logger.getLogger(TopNChartTableBuilder.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(TopNChartTableBuilder.class.getName()).log(Level.SEVERE, null, ex);
    }
    return generatedFiles;
}

From source file:com.termmed.utils.FileHelper.java

/**
 * Copy to./*from   w w w  .  j  av  a  2s. co  m*/
 *
 * @param inputFile the input file
 * @param outputFile the output file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void copyTo(File inputFile, File outputFile) throws IOException {

    FileInputStream fis = new FileInputStream(inputFile);
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    LineNumberReader reader = new LineNumberReader(isr);

    FileOutputStream fos = new FileOutputStream(outputFile);
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
    BufferedWriter bw = new BufferedWriter(osw);

    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
        bw.append(lineRead);
        bw.append("\r\n");
    }
    reader.close();
    bw.close();

}

From source file:gamlss.utilities.MatrixFunctions.java

/**
 * Write matrix values to CSV file.//from   w  ww .jav  a  2s . c om
 * @param cmd - path to the file
 * @param m - matrix to write
 */
public static void matrixWriteCSV(final String cmd, final BlockRealMatrix m) {
    try {
        // Create file 
        FileWriter fstream = new FileWriter(cmd, false);
        BufferedWriter out = new BufferedWriter(fstream);

        for (int i = 0; i < m.getRowDimension(); i++) {
            for (int j = 0; j < m.getColumnDimension(); j++) {
                out.write(Double.toString(m.getEntry(i, j)));
                if (j < m.getColumnDimension() - 1) {
                    out.append(',');
                }
            }
            out.newLine();
        }
        out.close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}