Example usage for java.io BufferedWriter flush

List of usage examples for java.io BufferedWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

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();/* ww 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:edu.iu.kmeans.regroupallgather.KMUtil.java

/**
 * Generate centroids and upload to the cDir
 * /*from   w  ww  .  j a  va  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] = 1000;
        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:Main.java

/**
 * Write an object that is already base64 encoded.
 *///from   w  w w. j  a  va 2s .  co  m
public static void writeBase64Object(BufferedWriter bw, String type, String object) throws IOException {

    bw.write("-----BEGIN ");
    bw.write(type);
    bw.write("-----");
    bw.newLine();

    bw.write(object);

    char lastChar = object.charAt(object.length() - 1);

    if (('\n' != lastChar) && ('\r' != lastChar)) {
        bw.newLine();
    }

    bw.write("-----END ");
    bw.write(type);
    bw.write("-----");
    bw.newLine();

    bw.flush();
}

From source file:com.yahoo.ycsb.db.RedisClient.java

public static String decompressWithLog(String st) {
    // System.out.println("decompress");
    // System.out.println(st.substring(0, 20));
    long start_time = System.nanoTime();
    String ret = decompress(st);//w  w  w.j  a  v a  2  s  .c  o  m
    try {
        long end_time = System.nanoTime();
        long time = end_time - start_time;
        BufferedWriter bw = new BufferedWriter(new FileWriter("decompress_time.txt", true));
        bw.write("" + time);
        bw.newLine();
        bw.flush();
    } catch (Exception e) {
    }
    // System.out.println("decompressed");
    // System.out.println(ret.substring(0, 20));
    return ret;
}

From source file:com.yahoo.ycsb.db.RedisClient.java

public static String compressWithLog(String st) {
    // System.out.println("compress");
    // System.out.println(st.substring(0, 20));
    long start_time = System.nanoTime();
    String ret = compress(st);/*from  w w  w.ja v  a 2s .  co m*/
    try {
        long end_time = System.nanoTime();
        long time = end_time - start_time;
        BufferedWriter bw = new BufferedWriter(new FileWriter("compress_time.txt", true));
        bw.write("" + time);
        bw.newLine();
        bw.flush();
        BufferedWriter bw2 = new BufferedWriter(new FileWriter("compress_rate.txt", true));
        double r1 = ret.length();
        double r2 = st.length();
        double r = r1 / r2;
        bw2.write("" + r);
        bw2.newLine();
        bw2.flush();
    } catch (Exception e) {
    }
    // System.out.println("compressed");
    // System.out.println(ret.substring(0, 20));
    return ret;
}

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 ww w .ja v a 2s. c  o m*/
    }

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

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

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  www. j  a  v  a  2s .  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:WarUtil.java

private static void writeLastModifiled(File file, long time) {
    BufferedWriter writer = null;
    try {/*  w ww  .j  a  v  a 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:Messenger.TorLib.java

/**
 * This method makes a http GET request for the specified resource to the specified hostname.
 * It uses the SOCKS proxy to a connection over Tor.
 * The DNS lookup is also done over Tor.
 * This method only uses port 443 for SSL.
 *
 * @param hostname hostname for target server.
 * @param port port to connect to./*from  ww  w  .j a v a 2  s.com*/
 * @param resource resource to lookup with GET request.
 * @return returns a JSON object.
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject getJSON(String hostname, int port, String resource)
        throws IOException, JSONException, HttpException {
    //Create a SSL socket using Tor
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    openSockets.add(sslSocket);

    //Create the HTTP GET request and push it over the outputstream
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("GET /" + resource + " HTTP/1.0\r\n");
    wr.write("Host: " + hostname + "\r\n");
    wr.write("\r\n");
    wr.flush();

    //Listen for a response on the inputstream
    BufferedReader br = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String t;
    boolean start = false;
    String output = "";
    while ((t = br.readLine()) != null) {
        if (t.equals("")) {
            start = true;
        }
        if (start) {
            output = output + t;
        }
    }
    br.close();
    wr.close();
    sslSocket.close();
    System.out.println(output);
    openSockets.remove(sslSocket);
    return new JSONObject(output);
}

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 ww  w.  j  av  a2  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;
}