Java Formatter ioException() 方法



描述

Java Formatter ioException() 方法返回此格式化程式的 Appendable 最後丟擲的 IOException。

如果目標的 append() 方法從未丟擲 IOException,則此方法將始終返回 null。

宣告

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

public IOException ioException()

引數

返回值

此方法返回 Appendable 最後丟擲的異常,如果不存在此類異常,則返回 null。

異常

檢查 US 本地化格式化程式物件的 IOException 示例

以下示例演示了 Java Formatter ioException() 方法的使用,用於在使用格式化程式時列印 Appendable 丟擲的 IOException。我們建立了一個帶有 StringBuffer 和 US 本地化的格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後我們列印了 ioException 物件。在本例中,它列印為 null。

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 latest exception, which is null
      System.out.println("" + formatter.ioException());
	  
	  formatter.close();
   }
}

輸出

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

Hello World !
null

檢查德語本地化格式化程式物件的 IOException 示例

以下示例演示了 Java Formatter ioException() 方法的使用,用於在使用格式化程式時列印 Appendable 丟擲的 IOException。我們建立了一個帶有 StringBuffer 和德語本地化的格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後我們列印了 ioException 物件。在本例中,它列印為 null。

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 latest exception, which is null
      System.out.println("" + formatter.ioException());
	  
	  formatter.close();
   }
}

輸出

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

Hello World !
null

檢查法語本地化格式化程式物件的 IOException 示例

以下示例演示了 Java Formatter ioException() 方法的使用,用於在使用格式化程式時列印 Appendable 丟擲的 IOException。我們建立了一個帶有 StringBuffer 和法語本地化的格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後我們列印了 ioException 物件。在本例中,它列印為 null。

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 latest exception, which is null
      System.out.println("" + formatter.ioException());
	  
	  formatter.close();
   }
}

輸出

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

Hello World !
null
java_util_formatter.htm
廣告