Java Formatter close() 方法



描述

Java Formatter close() 方法關閉此格式化程式。如果目標實現了 Closeable 介面,則將呼叫其 close 方法。關閉格式化程式允許它釋放它可能持有的資源(例如開啟的檔案)。如果格式化程式已關閉,則呼叫此方法無效。

宣告

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

public void close()

引數

返回值

此方法不返回值。

異常

關閉使用美國區域設定的 Formatter 物件示例

以下示例演示瞭如何使用 Java Formatter close() 方法關閉格式化程式。我們使用 StringBuffer 和美國區域設定建立了一個格式化程式物件。Formatter 用於列印字串,然後使用 close() 方法關閉。現在訪問格式化程式將導致異常,如示例所示。

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
      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.tutorialspoint.FormatterDemo.main(FormatterDemo.java:25)

關閉使用德國區域設定的 Formatter 物件示例

以下示例演示瞭如何使用 Java Formatter close() 方法關閉格式化程式。我們使用 StringBuffer 和德國區域設定建立了一個格式化程式物件。Formatter 用於列印字串,然後使用 close() 方法關閉。現在訪問格式化程式將導致異常,如示例所示。

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
      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.tutorialspoint.FormatterDemo.main(FormatterDemo.java:25)

關閉使用法國區域設定的 Formatter 物件示例

以下示例演示瞭如何使用 Java Formatter close() 方法關閉格式化程式。我們使用 StringBuffer 和法國區域設定建立了一個格式化程式物件。Formatter 用於列印字串,然後使用 close() 方法關閉。現在訪問格式化程式將導致異常,如示例所示。

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
      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.tutorialspoint.FormatterDemo.main(FormatterDemo.java:25)
java_util_formatter.htm
廣告

© . All rights reserved.