java.util.Formatter
類支持printf樣式格式化。
printf樣式格式化是C編程語言的良好支持。
以下代碼在Java中使用C的Printf樣式格式。
import java.util.Date; public class Main { public static void main(String[] args) { // Formatting strings System.out.printf("%1$s, %2$s, and %3$s %n", "ABC", "DEF", "XYZ"); System.out.printf("%3$s, %2$s, and %1$s %n", "ABC", "DEF", "XYZ"); // Formatting numbers System.out.printf("%1$4d, %2$4d, %3$4d %n", 1, 10, 100); System.out.printf("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000); System.out.printf("%1$-4d, %2$-4d, %3$-4d %n", 1, 10, 100); System.out.printf("%1$-4d, %2$-4d, %3$-4d %n", 10, 100, 1000); // Formatting date and time Date dt = new Date(); System.out.printf("Today is %tD %n", dt); System.out.printf("Today is %tF %n", dt); System.out.printf("Today is %tc %n", dt); } }
上面的代碼生成以下結(jié)果。
我們可以使用System.out.println()和System.out.print()方法在標(biāo)準(zhǔn)輸出上打印文本。
System.out是java.io.PrintStream類的一個(gè)實(shí)例,其中有println()和print()實(shí)例方法。
PrintStream類還有 format()
和 printf()
它可以支持printf樣式的格式化。
String類的靜態(tài)format()
也支持printf樣式的格式化。
PrintStream類的format()/printf()方法和String類的format()靜態(tài)方法的格式化行為是相同的。
PrintStream類中的format()/printf()方法將格式化的輸出寫入輸出流,而String類的format()方法返回格式化的輸出。
PrintStream類的format()/printf()方法和String類的format()靜態(tài)方法取決于Formatter類。
格式化程序用于格式化文本,可以寫入以下目的地:
以下代碼顯示如何使用Formatter類來格式化數(shù)據(jù)并將結(jié)果存儲(chǔ)在StringBuilder類中。
import java.util.Date; import java.util.Formatter; public class Main { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); Formatter fm = new Formatter(sb); // Formatting strings fm.format("%1$s, %2$s, and %3$s %n", "A", "B", "C"); fm.format("%3$s, %2$s, and %1$s %n", "D", "E", "F"); // Formatting numbers fm.format("%1$4d, %2$4d, %3$4d %n", 1, 10, 100); fm.format("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000); fm.format("%1$-4d, %2$-4d, %3$-4d %n", 1, 10, 100); fm.format("%1$-4d, %2$-4d, %3$-4d %n", 10, 100, 1000); // Formatting date and time Date dt = new Date(); fm.format("Today is %tD %n", dt); fm.format("Today is %tF %n", dt); fm.format("Today is %tc %n", dt); // Display the entire formatted string System.out.println(sb.toString()); } }
上面的代碼生成以下結(jié)果。
要將所有格式化的文本寫入文件,請(qǐng)使用以下代碼。
我們必須處理FileNotFoundException,如果指定的文件不存在,它可能會(huì)從Formatter類的構(gòu)造函數(shù)拋出。
我們必須調(diào)用它的close()方法來關(guān)閉輸出文件。
import java.io.File; import java.io.FileNotFoundException; import java.util.Formatter; public class Main { public static void main(String[] args) { File file = new File("xyz.txt"); Formatter fm = null; try { // Create a Formatter that will write the output the file fm = new Formatter(file); // Formatting strings fm.format("%1$s, %2$s, and %3$s %n", "A", "B", "C"); fm.format("%3$s, %2$s, and %1$s %n", "A", "B", "C"); // Format more text here... } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fm != null) { fm.close(); } } } }
Formatatter的format()方法是重載的。其聲明如下。
Formatter format(String format, Object... args) Formatter format(Locale l, String format, Object... args)
format()方法的第一個(gè)版本使用默認(rèn)語言環(huán)境進(jìn)行格式化。第二個(gè)版本允許您指定語言環(huán)境。
PrintStream類的format()/printf()方法和String類的format()方法支持format()方法的這兩個(gè)版本。
更多建議: