java closeutils_java.util.Formatter.close()方法实例

全屏

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评论

14487a65ea137d8f9ac97cdce44a0324.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值