Java.util.Formatter.toString()


描述

所述java.util.Formatter.toString()方法返回的目的地为输出调用的toString()的结果。

声明

以下是java.util.Formatter.toString()方法的声明

public String toString()

参数

NA

返回值

此方法返回在输出的目标上调用toString()的结果。

异常

FormatterClosedException - 如果已通过调用其close()方法关闭此formatter

实例

以下示例显示了java.util.Formatter.toString()方法的用法。

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 with default locale
      System.out.println("" + formatter);

      // print the formatter as a string
      System.out.println("" + formatter.toString());
   }
}

让我们编译并运行上面的程序,这将产生以下结果

Hello World !
Hello World !