当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。