Java IO Tutorial - Java BufferedWriter.flush()








Syntax

BufferedWriter.flush() has the following syntax.

public void flush()  throws IOException

Example

In the following code shows how to use BufferedWriter.flush() method.

//from   w ww .  j  a  v a  2  s.  c o  m

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class Main {
  public static void main(String[] args) throws IOException {

    String letters = "from java2s.com";

    StringWriter sw = new StringWriter();

    BufferedWriter bw = new BufferedWriter(sw);

    for (char c : letters.toCharArray()) {

      bw.append(c);

      bw.flush();

      System.out.println(sw.getBuffer());
    }

  }
}

The code above generates the following result.