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) 

Source Link

Document

Creates a buffered character-output stream that uses a default-sized output buffer.

Usage

From source file:com.caorongjin.pingguo.Pingguo.java

private static BufferedWriter _createWriter(String fileName) throws Exception {

    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileName), "UTF8");

    return new BufferedWriter(osw);
}

From source file:org.suren.autotest.web.framework.data.FreemarkerDynamicData.java

@Override
public String getValue(String orginData) {
    StringTemplateLoader templateLoader = new StringTemplateLoader();
    templateLoader.putTemplate(TMP_NAME, orginData);

    Configuration configuration = new Configuration();
    configuration.setTemplateLoader(templateLoader);
    configuration.setObjectWrapper(new DefaultObjectWrapper());
    configuration.setDefaultEncoding("UTF-8");

    try {/*w  w  w.j  a v a  2  s .  com*/
        Template template = configuration.getTemplate(TMP_NAME);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Writer writer = new BufferedWriter(new OutputStreamWriter(out));
        template.process(globalData, writer);

        return out.toString("utf-8");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TemplateException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:gov.nih.nci.cabig.caaers.utils.CaaersSerializerUtil.java

/**
 * This method writes given content to a xml file. Location of the file is $CATALINA_HOME/logs/serializedfiles/
 * @param serializedContent/* www  . java 2 s .  c  o m*/
 */
public static void dumpContentToFile(String content) {
    BufferedWriter out = null;
    StringBuilder sb = null;
    try {
        if (StringUtils.isEmpty(CATALINA_HOME)) {
            sb = new StringBuilder(USER_HOME);
        } else {
            sb = new StringBuilder(System.getenv("CATALINA_HOME"));
            sb.append("/logs");
        }
        sb.append("/serializedfiles");
        File file = new File(sb.toString());
        if (!file.isDirectory()) {
            file.mkdir();
        }
        sb.append("/session_").append(System.currentTimeMillis()).append(".xml");
        out = new BufferedWriter(new FileWriter(sb.toString()));
        out.write(content);
    } catch (Exception e) {
        logger.error("Exception while writing contect to file -- ", e);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.openmhealth.data.generator.service.FileSystemDataPointWritingServiceImpl.java

@Override
public long writeDataPoints(Iterable<? extends DataPoint<?>> dataPoints) throws IOException {

    long written = 0;

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {

        for (DataPoint dataPoint : dataPoints) {
            // this simplifies direct imports into MongoDB
            dataPoint.setAdditionalProperty("id", dataPoint.getHeader().getId());

            String valueAsString = objectMapper.writeValueAsString(dataPoint);
            writer.write(valueAsString);
            writer.write("\n");
            written++;// w  w  w  .j  a v  a2 s .c o m
        }
    }

    return written;
}

From source file:de.minestar.sixteenblocks.Threads.JSONThread.java

@SuppressWarnings("unchecked")
@Override/*from  w  ww .  j a v a  2  s  . c  om*/
public void run() {
    // Create sync thread to use notthreadsafe methods
    Bukkit.getScheduler().scheduleSyncDelayedTask(Core.getInstance(), new Runnable() {

        @Override
        public void run() {
            BufferedWriter bWriter = null;
            try {
                JSONObject json = new JSONObject();
                json.put("ConnectedUsers", Bukkit.getOnlinePlayers().length);
                json.put("Skins", aManager.getUsedAreaCount());
                json.put("Slots", Core.getAllowedMaxPlayer());
                bWriter = new BufferedWriter(new FileWriter(JSONFile));
                bWriter.write(json.toJSONString());
                bWriter.flush();
                bWriter.close();
            } catch (Exception e) {
                ConsoleUtils.printException(e, Core.NAME, "Can't save JSON");
            } finally {
                try {
                    if (bWriter != null)
                        bWriter.close();
                } catch (Exception e2) {
                    // IGNORE SECOND EXCEPTION
                }
            }
        }
    });

}

From source file:mattmc.mankini.commands.CommandQuote.java

private void addQuote(String content, MessageEvent<PircBotX> event) {
    try {/* w ww.  java  2  s  .  c  o m*/
        if (!file.exists()) {
            System.out.println(file.createNewFile());
        }

        FileWriter fw;

        fw = new FileWriter(file, true);

        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.newLine();
        bw.close();

        MessageSending.sendNormalMessage("Quote Added!", event);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.vital.vitalcep.cep.CepProcess.java

public void startCEP() throws FileNotFoundException, IOException {

    this.fileName = RandomStringUtils.randomAlphanumeric(8);

    try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(cepFolder//+"/"+dolceFile
            + "/" + fileName + "_dolce"), "utf-8"))) {
        writer.write(dolce);/*  w  w w .  j av  a2s  . c o  m*/
        writer.close();

        String cmd = cepFolder + "/bcep -d " + cepFolder + "/" + fileName + "_dolce -mi " + mqin + " -mo "
                + mqout + " -f " + cepFolder + "/" + confFile + " &>/dev/null &";

        logger.debug("starting bCEP with command: " + cmd);
        try {

            Process pr = Runtime.getRuntime().exec(cmd);

            is = pr.getInputStream();
            es = pr.getErrorStream();

            be = new Buffer_eraser();
            be.start();

            PID = getPid(pr);

            if (PID == -1) {
                java.util.logging.Logger.getLogger(CepProcess.class.getName()).log(Level.SEVERE,
                        "couldn't create the process");
                isUp = false;
            } else {
                isUp = true;
                logger.debug("new bCEP created: " + PID);
                logger.debug("mqin: " + mqin);
                logger.debug("mqout: " + mqout);
            }
        } catch (IOException e) {
            java.util.logging.Logger.getLogger(CepProcess.class.getName()).log(Level.SEVERE, e.getMessage());
            PID = -3;
            isUp = false;

        }
    } catch (IOException ex) {
        PID = -2;
        isUp = false;
        this.fileName = "";
        java.util.logging.Logger.getLogger(CepProcess.class.getName()).log(Level.SEVERE, ex.getMessage());
    }

}

From source file:seava.j4e.web.controller.ui.extjs.DependencyLoader.java

public void packFrameCmp(String bundle, String name, File file) throws Exception {

    List<String> list = new ArrayList<String>();
    this.resolveFrameDependencies(bundle, name, null, list, null);
    Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); // ISO-8859-1
    try {//  w w w.  j a v a 2s .  com
        for (String dep : list) {
            this.writeContentCmp(dep, writer);
        }
    } finally {
        writer.close();
    }
}

From source file:edu.psu.citeseerx.disambiguation.CsxDisambiguation.java

public static void createBlocks(ListableBeanFactory factory) throws Exception {
    String dirpath = "data/csauthors/blocks";

    DataSource dataSource = (DataSource) factory.getBean("csxDataSource");

    PreparedStatement st = dataSource.getConnection().prepareStatement("SELECT * FROM authors",
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    st.setFetchSize(Integer.MIN_VALUE);
    ResultSet rs = st.executeQuery();

    initDirectories(dirpath);//from  ww w .  j  a  v  a 2 s . c o m

    CsxAuthorFilter filter = (CsxAuthorFilter) factory.getBean("csxAuthorFilter");
    //new CsxAuthorFilter("data/csauthors/name_stopwords.txt");
    BufferedWriter skip = new BufferedWriter(new FileWriter("skip.txt"));

    int count = 0;
    Map<String, List<String>> blocks = new HashMap<String, List<String>>();
    while (rs.next()) {
        count++;
        if ((count % 10000) == 0)
            System.out.println("#Auth:" + count);
        String rsname = rs.getString("name");
        if (!filter.isStopword(rsname) && !filter.isInstitute(rsname) && !filter.isPosition(rsname)) {

            CsxAuthor auth = new CsxAuthor(rs);
            String lname = auth.getLastName();
            String fname = auth.getFirstName();

            if ((lname != null) && (fname != null)) {
                if ((lname.charAt(0) >= 'A') && (lname.charAt(0) <= 'Z') && (fname.charAt(0) >= 'A')
                        && (fname.charAt(0) <= 'Z') && !((fname.length() == 1) && (lname.length() == 1))
                        && !(lname.matches(".*/.*"))) {

                    String l_init = lname.substring(0, 1).toUpperCase();
                    String f_init = fname.substring(0, 1).toUpperCase();
                    String key = l_init + f_init + "/" + lname.toLowerCase() + "_" + f_init.toLowerCase()
                            + ".txt";

                    List<String> list;
                    if (!blocks.containsKey(key)) {
                        list = new ArrayList<String>();
                        blocks.put(key, list);
                    } else {
                        list = blocks.get(key);
                    }
                    list.add(auth.getId());
                } else {
                    skip.write("SKIP: [" + rsname + "]\n");
                }
            }
        }
    }
    skip.close();

    for (String key : blocks.keySet()) {
        List<String> aids = blocks.get(key);
        // only care about cluster with more than one document
        if (aids.size() > 1) {
            BufferedWriter out = new BufferedWriter(new FileWriter(dirpath + "/" + key));
            for (String aid : aids) {
                out.write(aid + "\n");
            }
            out.close();
        }
    }
}

From source file:CSVFileWriter.java

public CSVFileWriter(Writer writer, CSVFormat format) throws FileNotFoundException {
    this.writer = new BufferedWriter(writer);
    this.format = format;
}