Java.util.Formatter.flush() 方法
描述
java.util.Formatter.flush() 方法刷新此格式化程序。 如果目标实现了 Flushable 接口,则会调用其 flush 方法。 刷新格式化程序会将目标中的任何缓冲输出写入底层流。
声明
以下是 java.util.Formatter.flush() 方法的声明
public void flush()
参数
NA
返回值
此方法不返回值。
异常
FormatterClosedException − 如果此格式化程序已通过调用其 close() 方法关闭
示例
下面的例子展示了 java.util.Formatter.flush() 方法的使用。
package com.tutorialspoint;
import java.util.Formatter;
import java.util.Locale;
public class FormatterDemo {
public static void main(String[] args) {
// create a new formatter
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
// format a new string
String name = "World";
formatter.format("Hello %s !", name);
// print the formatted string
System.out.println("" + formatter);
// flush the formatter. Here it does nothing.
formatter.flush();
System.out.println("Formatter Flushed.");
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
Hello World ! Formatter Flushed.

