Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:gamlss.utilities.MatrixFunctions.java

/**
 * Write value  to CSV file.//from  ww  w.  j ava2s  .  c o 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:com.hmiard.blackwater.projects.Builder.java

/**
 * Deleting a server from a project.//from www . j  a v  a 2 s.c o  m
 *
 * @param projectRoot String
 * @param serverName String
 * @param listener ConsoleEmulator
 * @return Boolean
 */
public static Boolean deleteServer(String projectRoot, String serverName, ConsoleEmulator listener) {

    serverName = serverName.substring(0, 1).toUpperCase() + serverName.substring(1).toLowerCase();
    serverName += "Server";
    File checker = new File(projectRoot + "\\src\\" + serverName);
    File composerJSON = new File(projectRoot + "\\composer.json");

    if (!checker.exists()) {
        listener.push("This server is missing ! Operation aborted.");
        return false;
    }
    if (!composerJSON.exists()) {
        listener.push("File composer.json is missing ! Operation aborted.");
        return false;
    }

    try {
        FileUtils.deleteDirectory(checker);

        if (checker.exists()) {
            listener.push("Woops ! It seems impossible to delete " + checker.getAbsolutePath());
            return false;
        }

        JSONObject composer = new JSONObject(readFile(composerJSON.getAbsolutePath()));
        JSONObject autoload = composer.getJSONObject("autoload");
        JSONObject psr0 = autoload.getJSONObject("psr-0");

        psr0.remove(serverName);

        BufferedWriter cw = new BufferedWriter(new FileWriter(composerJSON.getAbsoluteFile()));
        String content = composer.toString(4).replaceAll("\\\\", "");
        cw.write(content);
        cw.close();

        listener.push("\n" + serverName + " was deleted successfully !\n");

    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

    return true;
}

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. ja v  a 2s .co m
    }

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

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

From source file:Main.java

/**
 * Print out the spike trains so it can be analyzed by plotting software
 * @param file//from   w w  w.  j av  a2s . com
 * @param array
 */
public static void printArray(File file, double[] x, double[] y) {
    FileWriter fstream = null;
    BufferedWriter out = null;

    try {
        fstream = new FileWriter(file);
        out = new BufferedWriter(fstream);
        for (int i = 0; i < x.length; i++) {
            out.write(x[i] + "\t");
        }
        out.write("\r\n");
        for (int i = 0; i < y.length; i++) {
            out.write(y[i] + "\t");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:com.thejustdo.util.Utils.java

/**
 * Given some data it creates a new file inside the servlet context.
 *
 * @param location Where to create the file.
 * @param ctx Gives us the real paths to be used.
 * @param content What to write./*from  w w  w .j a  v a  2  s .  c o  m*/
 * @param filename What name will have the new file.
 * @return The created file.
 * @throws IOException If any errors.
 */
public static File createNewFileInsideContext(String location, ServletContext ctx, String content,
        String filename) throws IOException {
    String real = ctx.getContextPath();
    String path = real + location + filename;
    log.info(String.format("File to save: %s", path));
    File f = new File(path);

    // 1. Creating the writers.
    FileWriter fw = new FileWriter(f);
    BufferedWriter writer = new BufferedWriter(fw);

    // 2. Writing contents.
    writer.write(content);

    // 3. Closing stream.
    writer.flush();
    writer.close();
    fw.close();

    return f;
}

From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java

private static String convertText(String string) {
    // here, the conversion takes place automatically,
    // thanks to Java
    StringReader reader = new StringReader(string);
    StringWriter writer = new StringWriter();
    try {//w  w w .  j  av  a 2s  .c  o  m
        BufferedReader bufferedreader = new BufferedReader(reader);
        BufferedWriter bufferedwriter = new BufferedWriter(writer);
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            bufferedwriter.write(line);
            bufferedwriter.newLine();
        }
        bufferedreader.close();
        bufferedwriter.close();
    } catch (IOException e) {/* HANDLE EXCEPTION */
    }
    return writer.toString();
}

From source file:com.github.jmabuin.blaspark.io.IO.java

public static void writeVectorToFileInHDFS(String file, DenseVector vector, Configuration conf) {

    try {/*from   w  w w . j  a  va 2  s  .co  m*/
        FileSystem fs = FileSystem.get(conf);

        Path pt = new Path(file);

        //FileSystem fileSystem = FileSystem.get(context.getConfiguration());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fs.create(pt, true)));

        bw.write("%%MatrixMarket matrix array real general");
        bw.newLine();
        bw.write(vector.size() + " 1");
        bw.newLine();

        for (int i = 0; i < vector.size(); i++) {
            bw.write(String.valueOf(vector.apply(i)));
            bw.newLine();
        }

        bw.close();
        //fs.close();

    } catch (IOException e) {
        LOG.error("Error in " + IO.class.getName() + ": " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }

}

From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java

/**
 * Generate centroids and upload to the cDir
 * //w  w w  .  j  ava  2s  .  c o  m
 * @param numCentroids
 * @param vectorSize
 * @param configuration
 * @param random
 * @param cenDir
 * @param fs
 * @throws IOException
 */
static void generateCentroids(int numCentroids, int vectorSize, Configuration configuration, Path cenDir,
        FileSystem fs) throws IOException {
    Random random = new Random();
    double[] data = null;
    if (fs.exists(cenDir))
        fs.delete(cenDir, true);
    if (!fs.mkdirs(cenDir)) {
        throw new IOException("Mkdirs failed to create " + cenDir.toString());
    }
    data = new double[numCentroids * vectorSize];
    for (int i = 0; i < data.length; i++) {
        data[i] = random.nextDouble() * 1000;
    }
    Path initClustersFile = new Path(cenDir, Constants.CENTROID_FILE_NAME);
    System.out.println("Generate centroid data." + initClustersFile.toString());
    FSDataOutputStream out = fs.create(initClustersFile, true);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
    for (int i = 0; i < data.length; i++) {
        if ((i % vectorSize) == (vectorSize - 1)) {
            bw.write(data[i] + "");
            bw.newLine();
        } else {
            bw.write(data[i] + " ");
        }
    }
    bw.flush();
    bw.close();
    System.out.println("Wrote centroids data to file");
}

From source file:com.aurel.track.lucene.util.FileUtil.java

public static void write(File file, String s, boolean lazy) throws IOException {

    if (file.getParent() != null) {
        mkdirs(file.getParent());//from  w w  w . jav  a 2 s.c o m
    }

    if (file.exists()) {
        String content = FileUtil.read(file);

        if (content.equals(s)) {
            return;
        }
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);

    bw.close();
}

From source file:biospectra.BioSpectra.java

private static void simulateReads(String[] args) throws Exception {
    String fastaDir = args[0];/*from  ww w.  ja v  a2  s . c  o m*/
    int readSize = Integer.parseInt(args[1]);
    double errorRatioStart = Double.parseDouble(args[2]);
    double errorRatioEnd = Double.parseDouble(args[3]);
    double errorRatioStep = Double.parseDouble(args[4]);
    int iteration = Integer.parseInt(args[5]);
    String outDir = args[6];

    List<File> fastaDocs = FastaFileHelper.findFastaDocs(fastaDir);

    File outDirFile = new File(outDir);
    if (!outDirFile.exists()) {
        outDirFile.mkdirs();
    }

    MetagenomicReadGenerator generator = new MetagenomicReadGenerator(fastaDocs);
    for (double cur = errorRatioStart; cur <= errorRatioEnd; cur += errorRatioStep) {
        File outFile = new File(outDir + "/sample_" + cur + ".fa");
        BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
        for (int i = 0; i < iteration; i++) {
            FASTAEntry sample = generator.generate(readSize, cur);
            bw.write(sample.getHeaderLine());
            bw.newLine();
            bw.write(sample.getSequence());
            bw.newLine();
        }
        bw.close();
    }
}