Java BufferedWriter.flush()方法範例

2019-10-16 22:17:39

Java BufferedWriter.flush()方法範例

BufferedWriterJava BufferedWriter.flush()方法的語法如下。

public void flush()  throws IOException

範例

在下面的程式碼中展示了如何使用BufferedWriter.flush()方法。

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 tw511.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());
    }

  }
}

上面的程式碼生成以下結果。

f
fr
fro
from
from 
from y
from yi
from yii
from yiib
from yiiba
from yiibai
from yiibai.
from yiibai.c
from yiibai.co
from tw511.com