Java 打印样式格式
Java格式 - Java 打印样式格式
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);
}
}
上面的代码生成以下结果。
格式化类
我们可以使用System.out.println()和System.out.print()方法在标准输出上打印文本。
System.out是java.io.PrintStream类的一个实例,其中有println()和print()实例方法。
PrintStream类还有 format()和 printf()它可以支持printf样式的格式化。
String类的静态format()也支持printf样式的格式化。
PrintStream类的format()/printf()方法和String类的format()静态方法的格式化行为是相同的。
PrintStream类中的format()/printf()方法将格式化的输出写入输出流,而String类的format()方法返回格式化的输出。
PrintStream类的format()/printf()方法和String类的format()静态方法取决于Formatter类。
格式化程序用于格式化文本,可以写入以下目的地:
- 附加(例如StringBuffer,StringBuilder,Writer等)
- 文件
- 输出流
- 打印流
以下代码显示如何使用Formatter类来格式化数据并将结果存储在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());
}
}
上面的代码生成以下结果。
格式化为文件
要将所有格式化的文本写入文件,请使用以下代码。
我们必须处理FileNotFoundException,如果指定的文件不存在,它可能会从Formatter类的构造函数抛出。
我们必须调用它的close()方法来关闭输出文件。
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()方法
Formatatter的format()方法是重载的。其声明如下。
Formatter format(String format, Object... args) Formatter format(Locale l, String format, Object... args)
format()方法的第一个版本使用默认语言环境进行格式化。第二个版本允许您指定语言环境。
PrintStream类的format()/printf()方法和String类的format()方法支持format()方法的这两个版本。

免费 AI IDE


更多建议: