Java BufferedWriter.flush()方法範例
BufferedWriter
的Java 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