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.android.dumprendertree2.FsUtils.java

public static void saveTestListToStorage(File file, int start, List<String> testList) {
    try {//from   w  w  w. ja v a 2s .  c  o m
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (String line : testList.subList(start, testList.size())) {
            writer.write(line + '\n');
        }
        writer.flush();
        writer.close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "failed to write test list", e);
    }
}

From source file:de.uzk.hki.da.sb.restfulService.FileUtil.java

/**
 * Method appends a String to File// w  ww. j a v a2 s . co m
 * @param fileName
 * @param contentString
 * @return
 */
public static String appendStringToResultFile(String fileName, String contentString) {
    FileWriter fw = null;
    try {
        inputFile = new File(Configuration.getResultDirPath() + fileName);
        fw = new FileWriter(inputFile, true);
        log.info(Configuration.getResultDirPath());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(contentString);
        bw.flush();
        bw.close();
    } catch (IOException ioExc) {
        log.error(ioExc);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    log.info("File-Size, Ergebnis: " + inputFile.length());
    return inputFile.getName();
}

From source file:com.cemso.util.CheckDeviceStateExcuter.java

public static void checkWait() {
    if (log.isInfoEnabled()) {
        log.info("auto refresh is waiting...");
        try {/*from  w  ww  .  j av a2s. c  o  m*/
            File f = new File("C:/DLLfunctionsTest.txt");
            FileWriter fw = new FileWriter(f, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.newLine();
            bw.append(new java.util.Date().toString() + ": auto refresh is waiting...");
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    checkerHandle1.cancel(true);
}

From source file:net.arccotangent.pacchat.net.Client.java

public static void sendMessage(String msg, String ip_address) {
    client_log.i("Sending message to " + ip_address);
    client_log.i("Connecting to server...");

    PublicKey pub;//ww  w .  ja v a2  s.co  m
    PrivateKey priv;
    Socket socket;
    BufferedReader input;
    BufferedWriter output;

    client_log.i("Checking for recipient's public key...");
    if (KeyManager.checkIfIPKeyExists(ip_address)) {
        client_log.i("Public key found.");
    } else {
        client_log.i("Public key not found, requesting key from their server.");
        try {
            Socket socketGetkey = new Socket();
            socketGetkey.connect(new InetSocketAddress(InetAddress.getByName(ip_address), Server.PORT), 1000);
            BufferedReader inputGetkey = new BufferedReader(
                    new InputStreamReader(socketGetkey.getInputStream()));
            BufferedWriter outputGetkey = new BufferedWriter(
                    new OutputStreamWriter(socketGetkey.getOutputStream()));

            outputGetkey.write("301 getkey");
            outputGetkey.newLine();
            outputGetkey.flush();

            String pubkeyB64 = inputGetkey.readLine();
            byte[] pubEncoded = Base64.decodeBase64(pubkeyB64);
            X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubEncoded);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            outputGetkey.close();
            inputGetkey.close();

            KeyManager.saveKeyByIP(ip_address, keyFactory.generatePublic(pubSpec));
        } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
            client_log.e("Error saving recipient's key!");
            e.printStackTrace();
        }
    }

    try {
        socket = new Socket();
        socket.connect(new InetSocketAddress(InetAddress.getByName(ip_address), Server.PORT), 1000);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    } catch (SocketTimeoutException e) {
        client_log.e("Connection to server timed out!");
        e.printStackTrace();
        return;
    } catch (ConnectException e) {
        client_log.e("Connection to server was refused!");
        e.printStackTrace();
        return;
    } catch (UnknownHostException e) {
        client_log.e("You entered an invalid IP address!");
        e.printStackTrace();
        return;
    } catch (IOException e) {
        client_log.e("Error connecting to server!");
        e.printStackTrace();
        return;
    }

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    pub = KeyManager.loadKeyByIP(ip_address);
    priv = Main.getKeypair().getPrivate();

    String cryptedMsg = MsgCrypto.encryptAndSignMessage(msg, pub, priv);
    try {
        client_log.i("Sending message to recipient.");
        output.write("200 encrypted message");
        output.newLine();
        output.write(cryptedMsg);
        output.newLine();
        output.flush();

        String ack = input.readLine();
        switch (ack) {
        case "201 message acknowledgement":
            client_log.i("Transmission successful, received server acknowledgement.");
            break;
        case "202 unable to decrypt":
            client_log.e(
                    "Transmission failure! Server reports that the message could not be decrypted. Did your keys change? Asking recipient for key update.");
            kuc_id++;
            KeyUpdateClient kuc = new KeyUpdateClient(kuc_id, ip_address);
            kuc.start();
            break;
        case "203 unable to verify":
            client_log.w("**********************************************");
            client_log.w(
                    "Transmission successful, but the receiving server reports that the authenticity of the message could not be verified!");
            client_log.w(
                    "Someone may be tampering with your connection! This is an unlikely, but not impossible scenario!");
            client_log.w(
                    "If you are sure the connection was not tampered with, consider requesting a key update.");
            client_log.w("**********************************************");
            break;
        case "400 invalid transmission header":
            client_log.e(
                    "Transmission failure! Server reports that the message is invalid. Try updating your software and have the recipient do the same. If this does not fix the problem, report the error to developers.");
            break;
        default:
            client_log.w("Server responded with unexpected code: " + ack);
            client_log.w("Transmission might not have been successful.");
            break;
        }

        output.close();
        input.close();
    } catch (IOException e) {
        client_log.e("Error sending message to recipient!");
        e.printStackTrace();
    }
}

From source file:com.hazelcast.qasonar.utils.Utils.java

public static void writeToFile(String fileName, StringBuilder content) throws IOException {
    FileWriter fileWriter = null;
    BufferedWriter writer = null;
    try {//from  ww w  .  j  a  va 2s .  com
        File file = new File(fileName);
        fileWriter = new FileWriter(file);
        writer = new BufferedWriter(fileWriter);
        writer.write(content.toString());
        writer.flush();
    } finally {
        closeQuietly(writer);
        closeQuietly(fileWriter);
    }
}

From source file:com.cemso.util.CheckDeviceStateExcuter.java

public static void checkStart() {
    if (log.isDebugEnabled()) {
        log.debug("CheckDeviceStateExcuter.checkStart()...");
    }/* w w w .j  a v a  2  s . c  o  m*/

    CheckRunner checker = new CheckDeviceStateExcuter().new CheckRunner();
    Thread t = new Thread(checker, "checker");
    checkerThread = t;

    final ScheduledFuture<?> checkerHandle = scheduler.scheduleAtFixedRate(t, 1, 2, TimeUnit.MINUTES);
    checkerHandle1 = checkerHandle;
    if (log.isInfoEnabled()) {
        log.info("auto refresh is started...");
        try {
            File f = new File("C:/DLLfunctionsTest.txt");
            FileWriter fw = new FileWriter(f, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.newLine();
            bw.append(new java.util.Date().toString() + ": auto refresh is started...");
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.github.jsonj.tools.JsonSerializer.java

/**
 * Writes the object out as json.//w w w  . j a  v a 2 s .c o m
 * 
 * @param out
 *            output writer
 * @param json
 *            a {@link JsonElement}
 * @param pretty
 *            if true, a properly indented version of the json is written
 * @throws IOException
 *             if there is a problem writing to the writer
 */
public static void serialize(final Writer out, final JsonElement json, final boolean pretty)
        throws IOException {
    BufferedWriter bw = new BufferedWriter(out);
    serialize(bw, json, pretty, 0);
    if (pretty) {
        out.write('\n');
    }
    bw.flush();
}

From source file:Main.java

public static void handleClientRequest(Socket socket) {
    try {//from  w w  w .  j av a 2 s .c  o  m
        BufferedReader socketReader = null;
        BufferedWriter socketWriter = null;
        socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String inMsg = null;
        while ((inMsg = socketReader.readLine()) != null) {
            System.out.println("Received from  client: " + inMsg);

            String outMsg = inMsg;
            socketWriter.write(outMsg);
            socketWriter.write("\n");
            socketWriter.flush();
        }
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:DiskIO.java

public static void saveStringInFile(File toFile, String insertString) throws IOException {
    BufferedWriter out;

    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "ISO-8859-1"));
    out.write(insertString);/*  w w w.ja  va 2  s  .c o  m*/
    out.flush();
    out.close();
}

From source file:io.cslinmiso.line.utils.Utility.java

/**
 * Java IO (java io write file)//  ww w . j  a va  2s.  c o  m
 * 
 * @param data
 * @param fileName
 * @throws IOException
 */
public static void writeFile(List<String> data, String fileName) throws IOException {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
    try {
        for (String d : data) {
            bw.write(d);
            bw.newLine();
        }
        bw.flush();
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        bw.close();
    }
}