close()方法是java.util.Formatter的內置方法,它將關閉格式化程序。如果它已經關閉,則將無效。關閉它意味著它將釋放它已經擁有的資源。
用法:
public void close()
參數:該函數不接受任何參數。
返回值:該函數不返回任何內容,僅關閉格式化程序。因此,返回類型為void。
下麵是上述函數的實現:
示例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);
// Prints the formatted string
// again since it is not closed
System.out.println(frmt);
// close the frmt
frmt.close();
}
}
輸出:
What is your name? My name is Gopal Dave ! What is your name? My name is Gopal Dave !
示例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 = "Java programs are fun";
frmt.format("%s !", name);
System.out.println(frmt);
// close the frmt
frmt.close();
}
}
輸出:
Java programs are fun !
參考: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html#close()
相關用法
- Java Formatter out()用法及代碼示例
- Java Formatter ioException()用法及代碼示例
- Java Formatter flush()用法及代碼示例
- Java Formatter locale()用法及代碼示例
- Java CharArrayReader close()用法及代碼示例
- Java ByteArrayInputStream close()用法及代碼示例
- Java CharArrayWriter close()用法及代碼示例
- Java StringWriter close()用法及代碼示例
- Java Scanner close()用法及代碼示例
- Java PrintWriter close()用法及代碼示例
- Java Reader close()用法及代碼示例
- Java PrintStream close()用法及代碼示例
- Java PushbackReader close()用法及代碼示例
- Java Writer close()用法及代碼示例
- Java ObjectInputStream close()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Formatter close() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。