void flush()
描述 (Description)
java.util.Formatter.flush()方法刷新此格式化程序。 如果目标实现Flushable接口,则将调用其flush方法。 刷新格式化程序会将目标中的任何缓冲输出写入基础流。
声明 (Declaration)
以下是java.util.Formatter.flush()方法的声明
public void flush()
参数 (Parameters)
NA
返回值 (Return Value)
此方法不返回值。
异常 (Exception)
FormatterClosedException - 如果已通过调用其close()方法关闭此formatter
例子 (Example)
以下示例显示了java.util.Formatter.flush()方法的用法。
package com.iowiki;
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.