全屏
java.util.Formatter.close()方法关闭此格式。如果目标实现Closeable接口,它的close方法将被调用。关闭格式化使得它能够释放资源可能会持有(如打开的文件)。如果格式化程序已经关闭,则调用此方法无效。
声明
以下是java.util.Formatter.close()方法的声明public void close()
参数NA
返回值
此方法不返回任何值。
异常NA
例子
下面的示例演示java.util.Formatter.close()方法的用法。package cn.sxt;
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);
// close the formatter
formatter.close();
// attempt to access the formatter results in exception
System.out.println("" + formatter);
}
}
让我们来编译和运行上面的程序,这将产生以下结果:Hello World !
Exception in thread "main" java.util.FormatterClosedException
at java.util.Formatter.ensureOpen(Formatter.java:2318)
at java.util.Formatter.toString(Formatter.java:2265)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at com.yiibai.FormatterDemo.main(FormatterDemo.java:25)
Java Result: 1
分享到:
0评论