當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Formatter ioException()用法及代碼示例


ioException()方法是java.util.Formatter的內置方法,它返回此格式化程序的Appendable最後拋出的IOException。如果目標的append()方法從不拋出IOException,則此方法將始終返回null。

用法

public IOException ioException()

參數:該函數不接受任何參數。


返回值:該函數返回IOException,它是Formatter的附加項最後拋出的異常。

下麵是上述函數的實現:

示例1:

// Java program to implement 
// the above function 
  
import java.util.Formatter; 
import java.util.Locale; 
  
public class Main { 
  
    public static void main(String[] args) 
    { 
  
        // Get the string Buffer 
        StringBuffer buffer 
            = new StringBuffer(); 
  
        // Object creation 
        Formatter frmt 
            = new Formatter(buffer, 
                            Locale.CANADA); 
  
        // Format a new string 
        String name = "My name is Gopal Dave"; 
        frmt.format("What is your name? \n%s !", 
                    name); 
  
        // Print the Formatted string 
        System.out.println(frmt); 
  
        // flushes the formatter 
        frmt.flush(); 
        System.out.println("Flushed"); 
    } 
}
輸出:
What is your name? 
My name is Gopal Dave !
Flushed

示例2:

// Java program to implement 
// the above function 
  
import java.util.Formatter; 
import java.util.Locale; 
  
public class Main { 
  
    public static void main(String[] args) 
    { 
  
        // Get the string Buffer 
        StringBuffer buffer 
            = new StringBuffer(); 
  
        // Object creation 
        Formatter frmt 
            = new Formatter(buffer, 
                            Locale.CANADA); 
  
        // Format a new string 
        String name = "My name is Gopal Dave"; 
        frmt.format("What is your name? \n%s !", 
                    name); 
  
        System.out.println("The last exception thrown: "
                           + frmt.ioException()); 
  
        // Closes the format 
        frmt.close(); 
    } 
}
輸出:
The last exception thrown: null
輸出:
The last exception thrown: null

參考: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#flush()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Formatter ioException() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。