Example usage for java.io BufferedWriter BufferedWriter

List of usage examples for java.io BufferedWriter BufferedWriter

Introduction

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

Prototype

public BufferedWriter(Writer out, int sz) 

Source Link

Document

Creates a new buffered character-output stream that uses an output buffer of the given size.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    String str = "from java2s.com!";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw, 200);

}

From source file:Main.java

public static void writeToFile(String fileName, String xml) throws IOException {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true), 1024));
    out.println(xml);//  w  w w. j a v  a  2 s. c o m
    out.flush();
    out.close();
}

From source file:Main.java

public static void writeToFile(final String str, final File file) {
    try {/* w  ww  .j  a v  a2s  . c  om*/
        final BufferedWriter output = new BufferedWriter(new FileWriter(file), 8192);
        output.write(str);
        output.close();
    } catch (final IOException exception) {
        exception.printStackTrace();
    }
}

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

/**
 *
 * @param exportData ?/*from www  .ja  v  a  2s .  co  m*/
 * @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;
}

From source file:com.pentacog.mctracker.SaveServerListTask.java

@Override
protected Void doInBackground(final ArrayList<Server>... params) {
    File serverFile = new File(context.getFilesDir(), MCServerTrackerActivity.SERVER_CACHE_FILE);
    try {//from  w  w w.  j  av a2  s .  com
        if (!serverFile.exists()) {
            serverFile.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(serverFile), 4000);

        JSONArray array = new JSONArray();
        for (Server server : params[0]) {
            array.put(server.toJSON());
        }

        bw.write(array.toString());
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    context = null;
    return null;
}

From source file:Main.java

public static void write(File file, String data) {
    BufferedWriter out = null;/*from   www .j  a  v a  2 s .co  m*/
    try {
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:edu.usc.goffish.gofs.formats.gml.GMLWriter.java

public GMLWriter(OutputStream output, boolean writeIndentation, int bufferSize) {
    _output = new BufferedWriter(new OutputStreamWriter(output, GMLParser.GML_CHARSET), bufferSize);
    _writeIndentation = writeIndentation;
    _indentation = 0;/*w  w w  .j a  v a  2  s . c  om*/
}

From source file:edu.caltech.ipac.firefly.server.util.DsvToDataGroup.java

public static void write(Writer writer, DataGroup data, CSVFormat format) throws IOException {

    BufferedWriter outf = new BufferedWriter(writer, IpacTableUtil.FILE_IO_BUFFER_SIZE);
    try {//  ww w  . j  a  v  a  2 s  .  com
        CSVPrinter printer = new CSVPrinter(outf, format);

        if (data != null && data.size() > 0) {
            for (DataType t : data.getDataDefinitions()) {
                printer.print(t.getKeyName());
            }
            printer.println();

            for (DataObject row : data.values()) {
                for (String s : row.getFormatedData()) {
                    printer.print(s.trim());
                }
                printer.println();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (outf != null) {
            outf.close();
        }
    }
}

From source file:com.exzogeni.dk.log.formatter.SimpleLogFormatter.java

@Override
public String format(@NonNull Thread thread, @NonNull StackTraceElement caller, @NonNull Throwable e) {
    final StringBuilder message = newMessageBuilder(thread, caller);
    final StringWriter trace = new StringWriter();
    final PrintWriter traceWriter = new PrintWriter(new BufferedWriter(trace, 512), true);
    try {/*  w  w w  . ja  v a2  s.  co m*/
        e.printStackTrace(traceWriter);
    } finally {
        traceWriter.flush();
        IOUtils.closeQuietly(traceWriter);
    }
    return message.append(trace.toString()).toString();
}

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.util.ThreadedBufferedWriterSlowTest.java

public void testCompareSpeed() throws IOException, InterruptedException {

    PrintWriter writer1 = null;/*from  www .ja  va2 s.c  o m*/
    PrintWriter writer2 = null;

    try {
        //noinspection IOResourceOpenedButNotSafelyClosed
        writer1 = new PrintWriter(new ThreadedBufferedWriter(new FileWriter(testOutputFile), BUFFER_SIZE));
        long before1 = System.currentTimeMillis();
        writeOutput(writer1);
        long after1 = System.currentTimeMillis();
        long diff1 = after1 - before1;
        System.out.println("Threaded: took " + diff1 + " milliseconds");
        //noinspection IOResourceOpenedButNotSafelyClosed
        writer2 = new PrintWriter(new BufferedWriter(new FileWriter(testCompareFile), BUFFER_SIZE));
        long before2 = System.currentTimeMillis();
        writeOutput(writer2);
        long after2 = System.currentTimeMillis();
        long diff2 = after2 - before2;
        System.out.println("Nonthreaded: took " + diff2 + " milliseconds");
        compareOutput();
        //assertTrue(diff2 >= diff1);
        System.out.println("Threaded is " + (diff2 - diff1) + " millis faster");
    } finally {
        IOUtils.closeQuietly(writer1);
        IOUtils.closeQuietly(writer2);
    }
}