Java IO Tutorial - Java DataOutputStream.flush()








Syntax

DataOutputStream.flush() has the following syntax.

public void flush()  throws IOException

Example

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

/*from   ww w  . java  2  s  . com*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

    String[] s = { "Hello", "java2s.com!" };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");

    DataOutputStream dos = new DataOutputStream(fos);

    for (String j : s) {
      dos.writeUTF(j);
    }

    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);
    while (dis.available() > 0) {
      String k = dis.readUTF();
      System.out.print(k);
    }

  }
}