Java Formatter out() 方法



描述

Java Formatter out() 方法返回輸出的目標。

宣告

以下是 java.util.Formatter.out() 方法的宣告

public Appendable out()

引數

返回值

此方法返回輸出的目標

異常

FormatterClosedException − 如果此格式化程式已透過呼叫其 close() 方法關閉

獲取美國區域設定的 Formatter 物件的輸出示例

以下示例演示瞭如何使用 Java Formatter out() 方法列印格式化程式的目標。我們使用 StringBuffer 和區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後使用 out() 方法列印其目標。

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 output
      System.out.println("" + formatter.out());
	  
      formatter.close();
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

Hello World !
Hello World !

獲取法語區域設定的 Formatter 物件的輸出示例

以下示例演示瞭如何使用 Java Formatter out() 方法列印格式化程式的目標。我們使用 StringBuffer 和法語區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後使用 out() 方法列印其目標。

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.FRENCH);

      // 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 output
      System.out.println("" + formatter.out());
	  
      formatter.close();
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

Hello World !
Hello World !

獲取德語區域設定的 Formatter 物件的輸出示例

以下示例演示瞭如何使用 Java Formatter out() 方法列印格式化程式的目標。我們使用 StringBuffer 和德語區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後使用 out() 方法列印其目標。

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.GERMAN);

      // 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 output
      System.out.println("" + formatter.out());
	  
      formatter.close();
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

Hello World !
Hello World !
java_util_formatter.htm
廣告

© . All rights reserved.