Example usage for java.io BufferedWriter write

List of usage examples for java.io BufferedWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:gemlite.core.util.RSAUtils.java

/**
 * ?key?//from   w w  w .  j av a  2 s . c o m
 * @param filePath
 * @return
 */
public static String writeKey(String filePath, String content) {
    StringBuilder sb = new StringBuilder();
    File file = new File(filePath);
    try {
        String charsetName = "utf-8";
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), charsetName);
        BufferedWriter pw = new BufferedWriter(writer);
        pw.write(content);
        pw.close();
    } catch (Exception e) {
        LogUtil.getCoreLog().warn("writeKey file :{} failure :{}", filePath, e);
    }
    return sb.toString();
}

From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static void writeStringToDiskFile(String data, String fullPath, String fileName) throws IOException {

    File outDir = new File(fullPath);

    if (!outDir.exists())
        outDir.mkdirs();/*from   w  w w .  j  a  v a 2s  .  co  m*/

    BufferedWriter out = null;

    try {
        FileWriter writer = new FileWriter(fullPath + fileName);
        out = new BufferedWriter(writer);
        out.write(data);
        out.flush();
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:it.polimi.diceH2020.plugin.control.DICEWrap.java

/**
 * Creates JSIM model (.jsimg) from pnml in the given directory  
 * //from  w ww  .  j a va  2s  .  com
 * @throws IOException
 */

public static void genJSIM(int cdid, String alt, String lastTransactionId) throws IOException {
    Configuration conf = Configuration.getCurrent();
    File sparkIdx = new File(Preferences.getSavingDir() + "spark.idx");
    String fileName;

    if (Configuration.getCurrent().getIsPrivate()) {
        fileName = Preferences.getSavingDir() + conf.getID() + "J" + cdid + "inHouse" + alt;
    } else {
        fileName = Preferences.getSavingDir() + conf.getID() + "J" + cdid + alt.replaceAll("-", "");
    }

    try {
        FileWriter fw = new FileWriter(sparkIdx.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(lastTransactionId);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String pnmlPath = Preferences.getSavingDir() + conf.getID() + "J" + cdid + alt.replaceAll("-", "")
            + ".pnml";
    String outputPath = new File(fileName + ".jsimg").getAbsolutePath();
    String indexPath = sparkIdx.getAbsolutePath();

    String command = String.format("java -cp %sbin:%slib/* PNML_Pre_Processor gspn %s %s %s",
            Preferences.getJmTPath(), Preferences.getJmTPath(), pnmlPath, outputPath, indexPath);

    System.out.println("Calling PNML_Pre_Processor");
    System.out.println(command);

    Process proc;

    try {
        proc = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    try {
        proc.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

From source file:gamlss.utilities.MatrixFunctions.java

/**
 * Write value  to CSV file./*w  w w . j ava 2  s  .  co m*/
 * @param cmd - path to the file
 * @param d - value
 */
public static void doubleWriteCSV(final String cmd, final double d, final boolean append) {

    try {
        // Create file 
        FileWriter fstream = new FileWriter(cmd, append);
        BufferedWriter out = new BufferedWriter(fstream);

        out.write(Double.toString(d));
        out.newLine();
        out.close();
    } catch (Exception e) { //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:WarUtil.java

private static void writeLastModifiled(File file, long time) {
    BufferedWriter writer = null;
    try {/*  ww w.j  ava 2 s .  c om*/
        if (!file.exists()) {
            file.createNewFile();
        }
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(Long.toString(time));
        writer.flush();
    } catch (Exception e) {

    }
}

From source file:dk.netarkivet.harvester.harvesting.frontier.FullFrontierReport.java

/**
 * Generates an Heritrix frontier report wrapper object by parsing the frontier report returned by the JMX
 * controller as a string./* w w  w.j a  va 2  s.c  o  m*/
 *
 * @param jobName the Heritrix job name
 * @param contentsAsString the text returned by the JMX call
 * @return the report wrapper object
 */
public static FullFrontierReport parseContentsAsString(String jobName, String contentsAsString) {

    FullFrontierReport report = new FullFrontierReport(jobName);

    // First dump this possibly huge string to a file
    File tmpDir = Settings.getFile(CommonSettings.CACHE_DIR);
    File tmpFile = new File(tmpDir, jobName + "-" + System.currentTimeMillis() + ".txt");
    try {
        tmpFile.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
        out.write(contentsAsString);
        out.close();
    } catch (IOException e) {
        LOG.error("Failed to create temporary file", e);
        return report;
    }

    BufferedReader br;
    try {
        br = new BufferedReader(new FileReader(tmpFile));
    } catch (FileNotFoundException e) {
        LOG.error("Failed to read temporary file", e);
        return report;
    }

    try {
        String lineToken = br.readLine(); // Discard header line
        while ((lineToken = br.readLine()) != null) {
            report.addLine(new FrontierReportLine(lineToken));
        }

        br.close();
    } catch (IOException e) {
        LOG.warn("Failed to close reader", e);
    } catch (Throwable t) {
        LOG.error(t);
        t.printStackTrace(System.err);
    } finally {
        FileUtils.remove(tmpFile);
    }

    return report;
}

From source file:org.envirocar.app.util.Util.java

public static void saveContentsToFile(String content, File f) throws IOException {
    if (!f.exists()) {
        f.createNewFile();/*from   w w  w. java2  s.  c  o m*/
    }

    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f, false));

    bufferedWriter.write(content);
    bufferedWriter.flush();
    bufferedWriter.close();
}

From source file:de.mpg.escidoc.services.syndication.Utils.java

/**
 * Writes <code>content</code> to the file
 * @param fileName/* w  w w.j  a v  a 2 s .  c om*/
 * @param content 
 * @throws IOException
 */
public static void writeToFile(String fileName, String content) throws IOException {
    FileWriter fw = new FileWriter(fileName);
    BufferedWriter out = new BufferedWriter(fw);
    out.write(content);
    out.close();
}

From source file:com.isa.utiles.Utiles.java

public static void downloadFile(String linkDescarga, String rutaDestino)
        throws MalformedURLException, IOException {

    URL urlFile = new URL(linkDescarga);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = new BufferedInputStream(urlFile.openStream());

    byte[] buf = new byte[1024];
    int n = 0;/*from w  w  w  . j  av a2  s  . c  o  m*/
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();

    /*
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(rutaDestino);
    fos.write(response);
    fos.close();
    */

    File file = new File(rutaDestino);
    file.setWritable(true);
    file.setReadable(true);

    BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
    bw.write(out.toString());
    bw.close();
}

From source file:de.fau.amos.FileDownload.java

/**
 * Creates a .csv file containing the data of the passed "ArrayList<ArrayList<String>> data" and saves it into the "userdir.location" directory
 * @param data/*  w  ww .  j ava 2 s  . c o  m*/
 * @param fileName
 */
private static void createCsvFile(ArrayList<ArrayList<String>> data, String fileName) {

    try {
        String lines = "";

        //using Buffered Writer to write a file into the "userdir.location" directory
        BufferedWriter bw = new BufferedWriter(
                new FileWriter(new File(System.getProperty("userdir.location"), fileName)));

        //every single value will be written down into the file separated by a semicolon
        for (int i = 0; i < data.size(); i++) {
            for (int j = 0; j < data.get(i).size(); j++) {
                lines = data.get(i).get(j) + "; ";
                bw.write(lines);
            }
            bw.newLine();

        }
        bw.flush();
        bw.close();

        System.out.println("Success!");
    } catch (IOException e) {
        System.out.println("Couldn't create File!");
    }
}