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.cybercat.automation.utils.CommonUtils.java

public static void appendToFile(String text, String filePath, String fileName) {
    // TODO file path should address to projects folder: TestResults

    FileWriter fWriter = null;/*from   w  ww . j  ava 2 s . c o  m*/
    BufferedWriter writer = null;

    try {
        new File(filePath).mkdirs();
        fWriter = new FileWriter(filePath + "/" + fileName, true);
        writer = new BufferedWriter(fWriter);
        writer.append(text);
        writer.newLine();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * Method appends a String to File/*from w ww. j av a2  s. c  om*/
 * @param fileName
 * @param contentString
 * @return
 */
public static String appendStringToResultFile(String fileName, String contentString) {
    FileWriter fw = null;
    try {
        inputFile = new File(Configuration.getResultDirPath() + fileName);
        fw = new FileWriter(inputFile, true);
        log.info(Configuration.getResultDirPath());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(contentString);
        bw.flush();
    } catch (IOException ioExc) {
        log.error(ioExc);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    log.info("File-Size, Ergebnis: " + inputFile.length());
    return inputFile.getName();
}

From source file:com.termmed.statistics.runner.Runner.java

/**
 * Save info.//from ww  w . ja  va 2 s.com
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void saveInfo() throws IOException {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    BufferedWriter bw = FileHelper
            .getWriter(new File(I_Constants.PROCESS_INFO_FOLDER, "analyticsResults.json"));

    bw.append(gson.toJson(OutputInfoFactory.get()).toString());

    bw.close();

}

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

private static void writeStatusByZone(Matrices statusMatrices, String zonalStatuses, String start, String end)
        throws ParseException {
    Date currentTime = STATUS_DATE_FORMAT.parse(start);
    Date endTime = STATUS_DATE_FORMAT.parse(end);
    Map<String, IdleStatusEntry> statuses = new TreeMap<String, IdleStatusEntry>();

    while (!currentTime.equals(endTime)) {
        Matrix m = statusMatrices.getMatrix(STATUS_DATE_FORMAT.format(currentTime));
        System.out.println(STATUS_DATE_FORMAT.format(currentTime));
        if (m == null) {
            System.err.println("time comes without status" + STATUS_DATE_FORMAT.format(currentTime));
            currentTime = getNextTime(currentTime);

            continue;
        }/*from   w  ww .  ja  va 2s .  com*/
        for (ArrayList<Entry> l : m.getFromLocations().values()) {

            for (Entry e : l) {
                if (!statuses.containsKey(e.getFromLocation())) {
                    statuses.put(e.getFromLocation(), new IdleStatusEntry(e.getFromLocation()));
                }
                IdleStatusEntry ent = statuses.get(e.getFromLocation());
                if (e.getToLocation().equals("65"))
                    ent.inc65(e.getValue());
                if (e.getToLocation().equals("66"))
                    ent.inc66(e.getValue());
                if (e.getToLocation().equals("70"))
                    ent.inc70(e.getValue());
                if (e.getToLocation().equals("75"))
                    ent.inc75(e.getValue());
                if (e.getToLocation().equals("79"))
                    ent.inc79(e.getValue());
                if (e.getToLocation().equals("80"))
                    ent.inc80(e.getValue());
                if (e.getToLocation().equals("83"))
                    ent.inc83(e.getValue());
                if (e.getToLocation().equals("85"))
                    ent.inc85(e.getValue());
                if (e.getToLocation().equals("87"))
                    ent.inc87(e.getValue());
                if (e.getToLocation().equals("90"))
                    ent.inc90(e.getValue());
            }
        }
        currentTime = getNextTime(currentTime);
    }
    BufferedWriter bw = IOUtils.getBufferedWriter(zonalStatuses);
    try {
        bw.append("zone\ts65\ts66\ts70\ts75\ts79\tss80\ts83\ts85\ts87\ts90\tsum (wait)");
        bw.newLine();
        for (IdleStatusEntry ise : statuses.values()) {
            bw.append(ise.toString());
            bw.newLine();
        }
        bw.flush();
        bw.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:de.uzk.hki.da.sb.restfulService.FileUtil.java

/**
 * Method appends a String to File//from www. j  a va 2s .co m
 * @param fileName
 * @param contentString
 * @return
 */
public static String appendStringToResultFile(String fileName, String contentString) {
    FileWriter fw = null;
    try {
        inputFile = new File(Configuration.getResultDirPath() + fileName);
        fw = new FileWriter(inputFile, true);
        log.info(Configuration.getResultDirPath());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(contentString);
        bw.flush();
        bw.close();
    } catch (IOException ioExc) {
        log.error(ioExc);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    log.info("File-Size, Ergebnis: " + inputFile.length());
    return inputFile.getName();
}

From source file:org.kuali.kfs.gl.batch.BatchSortUtil.java

private static int sortToTempFiles(String inputFileName, File tempSortDir, Comparator<String> comparator) {
    BufferedReader inputFile;/*from  w  ww. j  a va  2 s . c  o  m*/
    try {
        inputFile = new BufferedReader(new FileReader(inputFileName));
        //LOG.info("Successfully opened input file " + inputFileName);
    } catch (FileNotFoundException ex) {
        LOG.fatal("Unable to find input file: " + inputFileName, ex);
        throw new RuntimeException("Unable to find input file: " + inputFileName, ex);
    }
    try {
        String line = "";
        ArrayList<String> batchLines = new ArrayList<String>(linesPerFile);

        int numFiles = 0;
        while (line != null) {
            // get 10k rows
            for (int i = 0; i < linesPerFile; i++) {
                line = inputFile.readLine();
                if (line != null) {
                    batchLines.add(line);
                }
            }
            // sort the rows
            //                 batchLines = mergeSort(batchLines, comparator);
            Collections.sort(batchLines, comparator);

            // write to disk
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(tempSortDir, "chunk_" + numFiles)));
            for (int i = 0; i < batchLines.size(); i++) {
                bw.append(batchLines.get(i)).append('\n');
                //LOG.info("Writing temp sort file chunk_" + numFiles + " to tempSortDir " + tempSortDir);
            }
            bw.close();
            //LOG.info("Closed temp sort file chunk_" + numFiles);
            numFiles++;
            batchLines.clear(); // empty the array for the next pass
        }
        inputFile.close();
        //LOG.info("Successfully closed input file " + inputFileName);
        return numFiles;
    } catch (Exception ex) {
        LOG.fatal("Exception processing sort to temp files.", ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.github.seqware.queryengine.system.exporters.VCFDumper.java

/**
 * <p>dumpVCFFromFeatureSetID.</p>
 *
 * @param fSet a {@link com.github.seqware.queryengine.model.FeatureSet} object.
 * @param file a {@link java.lang.String} object.
 *//*from w  w  w.  jav a  2  s . c om*/
public static void dumpVCFFromFeatureSetID(FeatureSet fSet, String file) {
    BufferedWriter outputStream = null;

    try {
        if (file != null) {
            outputStream = new BufferedWriter(new FileWriter(file));
        } else {
            outputStream = new BufferedWriter(new OutputStreamWriter(System.out));
        }
        outputStream.append("#CHROM   POS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n");
    } catch (IOException e) {
        Logger.getLogger(VCFDumper.class.getName()).fatal("Exception thrown starting export to file:", e);
        System.exit(-1);
    }

    boolean caughtNonVCF = false;
    boolean mrSuccess = false;
    if (SWQEFactory.getQueryInterface() instanceof MRHBasePersistentBackEnd) {
        // hack to use VCF MR
        if (SWQEFactory.getModelManager() instanceof MRHBaseModelManager) {
            try {
                // pretend that the included com.github.seqware.queryengine.plugins.hbasemr.MRFeaturesByAttributesPlugin is an external plug-in
                Class<? extends PluginInterface> arbitraryPlugin;
                arbitraryPlugin = VCFDumperPlugin.class;
                // get a FeatureSet from the back-end
                QueryFuture<File> future = SWQEFactory.getQueryInterface().getFeaturesByPlugin(0,
                        arbitraryPlugin, fSet);
                File get = future.get();
                BufferedReader in = new BufferedReader(new FileReader(get));
                IOUtils.copy(in, outputStream);
                in.close();
                get.deleteOnExit();
                outputStream.flush();
                outputStream.close();
                mrSuccess = true;
            } catch (IOException e) {
                // fail out on IO error
                Logger.getLogger(VCFDumper.class.getName()).fatal("Exception thrown exporting to file:", e);
                System.exit(-1);
            } catch (Exception e) {
                Logger.getLogger(VCFDumper.class.getName())
                        .info("MapReduce exporting failed, falling-through to normal exporting to file");
                // fall-through and do normal exporting if Map Reduce exporting fails
            }
        } // TODO: clearly this should be expanded to include closing database etc 
    }
    if (mrSuccess) {
        return;
    }
    // fall-through if plugin-fails
    try {
        for (Feature feature : fSet) {
            StringBuffer buffer = new StringBuffer();
            boolean caught = outputFeatureInVCF(buffer, feature);
            if (caught) {
                caughtNonVCF = true;
            }
            outputStream.append(buffer);
            outputStream.newLine();
        }
        outputStream.flush();
    } catch (IOException e) {
        Logger.getLogger(VCFDumper.class.getName()).fatal("Exception thrown exporting to file:", e);
        System.exit(-1);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.ushahidi.android.app.util.Util.java

/**
 * For debugging purposes. Append content of a string to a file
 * //w  ww  .  j  a  v  a 2s .  c o  m
 * @param text
 */
public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory(), "ush_log.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:org.mapsforge.map.writer.automatization.OsmosisRunner.java

private static void writeLog(String logfile, Process process, boolean append) {

    BufferedWriter bw = null;
    BufferedReader br = null;// ww w  .ja  v a2s.c  o  m
    try {
        bw = new BufferedWriter(new FileWriter(logfile, append));
        br = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        String line = null;

        while ((line = br.readLine()) != null) {
            bw.append(line).append(NEWLINE);
            bw.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (br != null)
                br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.github.seqware.queryengine.system.Utility.java

/**
 * <p>dumpVCFFromFeatureSetID.</p>
 *
 * @param fSet a {@link com.github.seqware.queryengine.model.FeatureSet} object.
 * @param file a {@link java.lang.String} object.
 *//*w  w  w  .  j  a  v  a 2s.co m*/
public static boolean dumpFromMapReducePlugin(String header, Reference ref, FeatureSet fSet,
        Class<? extends PluginInterface> arbitraryPlugin, String file, Object... params) {
    BufferedWriter outputStream = null;
    try {
        if (file != null) {
            outputStream = new BufferedWriter(new FileWriter(file));
        } else {
            outputStream = new BufferedWriter(new OutputStreamWriter(System.out));
        }
        if (header != null) {
            outputStream.append(header);
        }
    } catch (IOException e) {
        Logger.getLogger(Utility.class.getName()).fatal("Exception thrown starting export to file:", e);
        System.exit(-1);
    }
    if (SWQEFactory.getQueryInterface() instanceof MRHBasePersistentBackEnd) {
        if (SWQEFactory.getModelManager() instanceof MRHBaseModelManager) {
            try {
                QueryFuture<File> future = SWQEFactory.getQueryInterface().getFeaturesByPlugin(0,
                        arbitraryPlugin, ref, params);
                File get = future.get();
                Collection<File> listFiles = FileUtils.listFiles(get, new WildcardFileFilter("part*"),
                        DirectoryFileFilter.DIRECTORY);
                for (File f : listFiles) {
                    BufferedReader in = new BufferedReader(new FileReader(f));
                    IOUtils.copy(in, outputStream);
                    in.close();
                }
                get.deleteOnExit();
                assert (outputStream != null);
                outputStream.flush();
                outputStream.close();
                return true;
            } catch (IOException e) {
                Logger.getLogger(VCFDumper.class.getName()).fatal("Exception thrown exporting to file:", e);
                System.exit(-1);
            } catch (Exception e) {
                Logger.getLogger(VCFDumper.class.getName())
                        .fatal("MapReduce exporting failed, falling-through to normal exporting to file", e);
            }
        }
    }
    return false;
}