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:edu.pitt.dbmi.ipm.service.EntityStreamingOutput.java

/**
 * Writes to stream./*from w w w  . j a  va  2  s  .  co  m*/
 */
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {

    byte[] buffer = new byte[1024];
    int read = -1;
    InputStream instream = null;

    try {

        instream = entity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(instream));
        Writer writer = new BufferedWriter(new OutputStreamWriter(output));

        String line = null;
        while ((line = br.readLine()) != null) {
            line = line.replaceAll("\"", "");
            writer.write(line + "\n");
            writer.flush();
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {

        if (instream != null)
            instream.close();

        if (output != null)
            output.close();

        httpclient.getConnectionManager().shutdown();
    }

}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.geneExpression.SetAttribute1Listener.java

@Override
public void handleEvent(Event event) {
    // TODO Auto-generated method stub
    Vector<String> values = this.setAttribute1UI.getValues();
    Vector<String> samples = this.setAttribute1UI.getSamples();
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".subject_mapping.tmp");
    File stsmf = ((GeneExpressionData) this.dataType).getStsmf();
    if (stsmf == null) {
        this.setAttribute1UI.displayMessage("Error: no subject to sample mapping file");
    }/*from ww  w  . jav  a  2 s.  c om*/
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write(
                "study_id\tsite_id\tsubject_id\tSAMPLE_ID\tPLATFORM\tTISSUETYPE\tATTR1\tATTR2\tcategory_cd\n");

        try {
            BufferedReader br = new BufferedReader(new FileReader(stsmf));
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                String sample = fields[3];
                String attribute;
                if (samples.contains(sample)) {
                    attribute = values.get(samples.indexOf(sample));
                } else {
                    br.close();
                    return;
                }
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + sample + "\t" + fields[4]
                        + "\t" + fields[5] + "\t" + attribute + "\t" + fields[7] + "\t" + fields[8] + "\n");
            }
            br.close();
        } catch (Exception e) {
            setAttribute1UI.displayMessage("File error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (stsmf != null) {
                String fileName = stsmf.getName();
                stsmf.delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".subject_mapping");
            }
            FileUtils.moveFile(file, fileDest);
            ((GeneExpressionData) this.dataType).setSTSMF(fileDest);
        } catch (IOException ioe) {
            this.setAttribute1UI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.setAttribute1UI.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setAttribute1UI.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.geneExpression.SetAttribute2Listener.java

@Override
public void handleEvent(Event event) {
    // TODO Auto-generated method stub
    Vector<String> values = this.setAttribute2UI.getValues();
    Vector<String> samples = this.setAttribute2UI.getSamples();
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".subject_mapping.tmp");
    File stsmf = ((GeneExpressionData) this.dataType).getStsmf();
    if (stsmf == null) {
        this.setAttribute2UI.displayMessage("Error: no subject to sample mapping file");
    }/*from w  w w  .  j a  va2  s .  c  o  m*/
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write(
                "study_id\tsite_id\tsubject_id\tSAMPLE_ID\tPLATFORM\tTISSUETYPE\tATTR1\tATTR2\tcategory_cd\n");

        try {
            BufferedReader br = new BufferedReader(new FileReader(stsmf));
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                String sample = fields[3];
                String attribute;
                if (samples.contains(sample)) {
                    attribute = values.get(samples.indexOf(sample));
                } else {
                    br.close();
                    return;
                }
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + sample + "\t" + fields[4]
                        + "\t" + fields[5] + "\t" + fields[6] + "\t" + attribute + "\t" + fields[8] + "\n");
            }
            br.close();
        } catch (Exception e) {
            this.setAttribute2UI.displayMessage("File error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (stsmf != null) {
                String fileName = stsmf.getName();
                stsmf.delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".subject_mapping");
            }
            FileUtils.moveFile(file, fileDest);
            ((GeneExpressionData) this.dataType).setSTSMF(fileDest);
        } catch (IOException ioe) {
            this.setAttribute2UI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.setAttribute2UI.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setAttribute2UI.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.geneExpression.SetTissueTypeListener.java

@Override
public void handleEvent(Event event) {
    // TODO Auto-generated method stub
    Vector<String> values = this.setTissueTypeUI.getValues();
    Vector<String> samples = this.setTissueTypeUI.getSamples();
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".subject_mapping.tmp");
    File stsmf = ((GeneExpressionData) this.dataType).getStsmf();
    if (stsmf == null) {
        this.setTissueTypeUI.displayMessage("Error: no subject to sample mapping file");
    }/*from w ww  . j  a v  a2  s  .co m*/
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write(
                "study_id\tsite_id\tsubject_id\tSAMPLE_ID\tPLATFORM\tTISSUETYPE\tATTR1\tATTR2\tcategory_cd\n");

        try {
            BufferedReader br = new BufferedReader(new FileReader(stsmf));
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                String sample = fields[3];
                String tissueType;
                if (samples.contains(sample)) {
                    tissueType = values.get(samples.indexOf(sample));
                } else {
                    br.close();
                    return;
                }
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + sample + "\t" + fields[4]
                        + "\t" + tissueType + "\t" + fields[6] + "\t" + fields[7] + "\t" + fields[8] + "\n");
            }
            br.close();
        } catch (Exception e) {
            this.setTissueTypeUI.displayMessage("File error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (stsmf != null) {
                String fileName = stsmf.getName();
                stsmf.delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".subject_mapping");
            }
            FileUtils.moveFile(file, fileDest);
            ((GeneExpressionData) this.dataType).setSTSMF(fileDest);
        } catch (IOException ioe) {
            this.setTissueTypeUI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.setTissueTypeUI.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setTissueTypeUI.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:DiskIO.java

public static void saveStringInFile(File toFile, String insertString) throws IOException {
    BufferedWriter out;/*from   w  w w . j av  a 2 s.  co  m*/

    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "ISO-8859-1"));
    out.write(insertString);
    out.flush();
    out.close();
}

From source file:edu.indiana.d2i.htrc.util.ClusterInspection.java

@Override
public int run(String[] args) throws Exception {
    String input = args[0]; // cluster path
    String output = args[1];// w  w w . ja  v a2  s  . c o  m

    int numVector = 0;

    Configuration conf = getConf();
    FileSystem fs = FileSystem.get(conf);
    FileStatus[] status = fs.listStatus(new Path(input), Utilities.HIDDEN_FILE_FILTER);
    Text key = new Text();
    LongWritable value = new LongWritable();
    BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    for (int i = 0; i < status.length; i++) {
        SequenceFile.Reader seqReader = new SequenceFile.Reader(fs, status[i].getPath(), conf);
        while (seqReader.next(key, value)) {
            numVector++;
            writer.write(key.toString() + "\n");
        }
    }
    writer.close();

    logger.info("#vector: " + numVector);

    return 0;
}

From source file:jatoo.exec.InputStreamExhausterWithDumpStream.java

public final void exhaust() {

    BufferedReader processInputStreamReader = null;
    BufferedWriter dumpOutputStreamWriter = null;

    try {/*from w w  w  .  j  a  v  a 2s . com*/

        processInputStreamReader = new BufferedReader(new InputStreamReader(processInputStream));
        dumpOutputStreamWriter = new BufferedWriter(new OutputStreamWriter(dumpOutputStream));

        String line = null;
        while ((line = processInputStreamReader.readLine()) != null) {

            dumpOutputStreamWriter.write(line);
            dumpOutputStreamWriter.newLine();

            dumpOutputStreamWriter.flush();
        }
    }

    catch (IOException e) {
        LOGGER.error("error exhausting the stream", e);
    }

    finally {
        if (closeDumpOutputStream && dumpOutputStreamWriter != null) {
            try {
                dumpOutputStreamWriter.close();
            } catch (IOException e) {
                LOGGER.error("error closing the dump stream", e);
            }
        }
    }
}

From source file:edu.indiana.d2i.htrc.util.TokensInspection.java

@Override
public int run(String[] args) throws Exception {
    String input = args[0]; // word count path
    String output = args[1];//w w  w  . j ava  2s . c o  m

    int numVector = 0;

    Configuration conf = getConf();
    FileSystem fs = FileSystem.get(conf);
    FileStatus[] status = fs.listStatus(new Path(input), Utilities.HIDDEN_FILE_FILTER);
    Text key = new Text();
    LongWritable value = new LongWritable();
    BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    for (int i = 0; i < status.length; i++) {
        SequenceFile.Reader seqReader = new SequenceFile.Reader(fs, status[i].getPath(), conf);
        while (seqReader.next(key, value)) {
            numVector++;
            writer.write(key.toString() + "\n");
        }
    }
    writer.close();

    logger.info("#vector: " + numVector);

    return 0;
}

From source file:org.jboss.as.test.manualmode.web.valve.authenticator.ValveUtil.java

public static void createValveModule(final ManagementClient managementClient, String modulename,
        String baseModulePath, String jarName, Class valveClass) throws Exception {
    log.info("Creating a valve module " + modulename);
    String path = ValveUtil.readASPath(managementClient.getControllerClient());
    File file = new File(path);
    if (file.exists()) {
        file = new File(path + baseModulePath);
        file.mkdirs();/*from w  ww .jav a  2  s  .c  om*/
        file = new File(path + baseModulePath + "/" + jarName);
        if (file.exists()) {
            file.delete();
        }
        createJar(file, valveClass);
        file = new File(path + baseModulePath + "/module.xml");
        if (file.exists()) {
            file.delete();
        }
        FileWriter fstream = new FileWriter(path + baseModulePath + "/module.xml");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("<module xmlns=\"urn:jboss:module:1.1\" name=\"" + modulename + "\">\n");
        out.write("    <properties>\n");
        out.write("        <property name=\"jboss.api\" value=\"private\"/>\n");
        out.write("    </properties>\n");

        out.write("    <resources>\n");
        out.write("        <resource-root path=\"" + jarName + "\"/>\n");
        out.write("    </resources>\n");

        out.write("    <dependencies>\n");
        out.write("        <module name=\"sun.jdk\"/>\n");
        out.write("        <module name=\"javax.servlet.api\"/>\n");
        out.write("        <module name=\"org.jboss.as.web\"/>\n");
        out.write("       <module name=\"org.jboss.logging\"/>\n");
        out.write("    </dependencies>\n");
        out.write("</module>");
        out.close();
    }
}

From source file:gr.uoc.nlp.opinion.ilsptagger.POSTagDocuments.java

private void crateTxtFiles(ResultSet rs) {

    System.out.println("Creating .txt files..");
    try {/* w ww.  ja v  a2s  .com*/
        while (rs.next()) {
            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(this.dir + rs.getString("comment_id") + ".txt"), "utf-8"))) {
                bw.write(rs.getString("comment"));

                bw.close();
            }
        }

        System.out.println("Done creating .txt files!");

    } catch (FileNotFoundException | UnsupportedEncodingException ex) {
        System.err.println("Abort!");
        Logger.getLogger(POSTagDocuments.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException | IOException ex) {
        Logger.getLogger(POSTagDocuments.class.getName()).log(Level.SEVERE, null, ex);
    }
}