Java : Formatter (String Syntax) 示例

Formatter (Java SE 22 & JDK 22) 示例。
您将在大多数 Formatter 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

printf 样式格式字符串的解释器。此类支持布局对齐和对齐、数字、字符串和日期/时间数据的通用格式以及特定于语言环境的输出。 (机器翻译)

Class diagram

try (final var formatter = new Formatter()) {
    formatter.format("str = %s : num = %d", "abc", 1234);

    final var str = formatter.toString();
    System.out.println(str); // str = abc : num = 1234
}
try (final var formatter = new Formatter()) {
    final var value = "abc";
    formatter.format("%s : %S", value, value);

    final var str = formatter.toString();
    System.out.println(str); // abc : ABC
}

try (final var formatter = new Formatter()) {
    final var value = 'x';
    formatter.format("%c : %C", value, value);

    final var str = formatter.toString();
    System.out.println(str); // x : X
}

try (final var formatter = new Formatter()) {
    final var value = 255;
    formatter.format("%d : %x : %X", value, value, value);

    final var str = formatter.toString();
    System.out.println(str); // 255 : ff : FF
}

try (final var formatter = new Formatter()) {
    final var value = 0.12345;
    formatter.format("%e : %f", value, value);

    final var str = formatter.toString();
    System.out.println(str); // 1.234500e-01 : 0.123450
}
try (final var formatter = new Formatter()) {
    formatter.format("%1$d : %2$d : %3$d", 111, 222, 333);
    System.out.println(formatter); // 111 : 222 : 333
}

try (final var formatter = new Formatter()) {
    formatter.format("%3$d : %2$d : %1$d", 111, 222, 333);
    System.out.println(formatter); // 333 : 222 : 111
}

Constructors

Formatter ()

构造一个新的格式化程序。 (机器翻译)

try (final var formatter = new Formatter()) {
    formatter.format("%s : %d", "abc", 1234);
    System.out.println(formatter); // abc : 1234
}

Formatter (File file)

使用指定的文件构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

try (final var formatter = new Formatter(file.toFile())) {
    formatter.format("%s : %d", "abc", 1234);
}

System.out.println(Files.readString(file)); // abc : 1234

Formatter (File file, String csn)

使用指定的文件和字符集构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

final var csn = "Shift_JIS";

try (final var formatter = new Formatter(file.toFile(), csn)) {
    formatter.format("%s : %d", "○△×", 1234);
}

System.out.println(Files.readString(file, Charset.forName(csn))); // ○△× : 1234

Formatter (File file, String csn, Locale l)

使用指定的文件、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

final var csn = StandardCharsets.UTF_8.name();
System.out.println(csn); // UTF-8

try (final var formatter = new Formatter(file.toFile(), csn)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, Charset.forName(csn))); // Sunday

try (final var formatter = new Formatter(file.toFile(), csn, Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, Charset.forName(csn))); // 日曜日

Formatter (File file, Charset charset, Locale l)

使用指定的文件、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

final var charset = StandardCharsets.UTF_8;

try (final var formatter = new Formatter(file.toFile(), charset, Locale.US)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, charset)); // Sunday

try (final var formatter = new Formatter(file.toFile(), charset, Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, charset)); // 日曜日

Formatter (OutputStream os)

用指定的输出流构造一个新的格式化程序。 (机器翻译)

final var os = new ByteArrayOutputStream();
try (final var formatter = new Formatter(os)) {
    formatter.format("%s : %d", "abc", 1234);
}

System.out.println(os); // abc : 1234

Formatter (OutputStream os, String csn)

使用指定的输出流和字符集构造一个新的格式化程序。 (机器翻译)

final var csn = "Shift_JIS";

final var os = new ByteArrayOutputStream();
try (final var formatter = new Formatter(os, csn)) {
    formatter.format("%s : %d", "○△×", 1234);
}

System.out.println(os.toString(csn)); // ○△× : 1234

Formatter (OutputStream os, String csn, Locale l)

使用指定的输出流、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var csn = StandardCharsets.UTF_8.name();
System.out.println(csn); // UTF-8

{
    final var os = new ByteArrayOutputStream();
    try (final var formatter = new Formatter(os, csn)) {
        formatter.format("%tA", DayOfWeek.SUNDAY);
    }
    System.out.println(os.toString(csn)); // Sunday
}
{
    final var os = new ByteArrayOutputStream();
    try (final var formatter = new Formatter(os, csn, Locale.JAPAN)) {
        formatter.format("%tA", DayOfWeek.SUNDAY);
    }
    System.out.println(os.toString(csn)); // 日曜日
}

Formatter (OutputStream os, Charset charset, Locale l)

使用指定的输出流、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

final var charset = StandardCharsets.UTF_8;

final var os = new ByteArrayOutputStream();
try (final var formatter = new Formatter(os, charset, Locale.US)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(os.toString(charset)); // Sunday
final var charset = StandardCharsets.UTF_8;

final var os = new ByteArrayOutputStream();
try (final var formatter = new Formatter(os, charset, Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(os.toString(charset)); // 日曜日

Formatter (PrintStream ps)

使用指定的打印流构造一个新的格式化程序。 (机器翻译)

final var out = new ByteArrayOutputStream();
try (final var ps = new PrintStream(out)) {

    try (final var formatter = new Formatter(ps)) {
        formatter.format("%s : %d", "abc", 1234);
    }
}

System.out.println(out); // abc : 1234

Formatter (Appendable a)

构造一个具有指定目标的新格式化程序。 (机器翻译)

final var a = new StringBuilder();
try (final var formatter = new Formatter(a)) {
    formatter.format("%s : %d", "abc", 1234);
}

System.out.println(a); // abc : 1234

Formatter (Appendable a, Locale l)

构造具有指定目标和语言环境的新格式化程序。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

{
    final var a = new StringBuilder();
    try (final var formatter = new Formatter(a)) {
        formatter.format("%tA", DayOfWeek.SUNDAY);
    }
    System.out.println(a); // Sunday
}
{
    final var a = new StringBuilder();
    try (final var formatter = new Formatter(a, Locale.JAPAN)) {
        formatter.format("%tA", DayOfWeek.SUNDAY);
    }
    System.out.println(a); // 日曜日
}

Formatter (String fileName)

用指定的文件名构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
final var fileName = file.toString();
System.out.println(fileName); // R:\java-work\aaa.txt

try (final var formatter = new Formatter(fileName)) {
    formatter.format("%s : %d", "abc", 1234);
}

System.out.println(Files.readString(file)); // abc : 1234

Formatter (String fileName, String csn)

用指定的文件名和字符集构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
final var fileName = file.toString();
System.out.println(fileName); // R:\java-work\aaa.txt

final var csn = "Shift_JIS";

try (final var formatter = new Formatter(fileName, csn)) {
    formatter.format("%s : %d", "○△×", 1234);
}

System.out.println(Files.readString(file, Charset.forName(csn))); // ○△× : 1234

Formatter (String fileName, String csn, Locale l)

使用指定的文件名、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

final var file = Path.of("R:", "java-work", "aaa.txt");
final var fileName = file.toString();
System.out.println(fileName); // R:\java-work\aaa.txt

final var csn = StandardCharsets.UTF_8.name();
System.out.println(csn); // UTF-8

try (final var formatter = new Formatter(fileName, csn)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, Charset.forName(csn))); // Sunday

try (final var formatter = new Formatter(fileName, csn, Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, Charset.forName(csn))); // 日曜日

Formatter (String fileName, Charset charset, Locale l)

使用指定的文件名、字符集和语言环境构造一个新的格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
final var fileName = file.toString();
System.out.println(fileName); // R:\java-work\aaa.txt

final var charset = StandardCharsets.UTF_8;

try (final var formatter = new Formatter(fileName, charset, Locale.US)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, charset)); // Sunday

try (final var formatter = new Formatter(fileName, charset, Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
}
System.out.println(Files.readString(file, charset)); // 日曜日

Formatter (Locale l)

构造具有指定语言环境的新格式化程序。 (机器翻译)

try (final var formatter = new Formatter(Locale.US)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // Sunday
}

try (final var formatter = new Formatter(Locale.JAPAN)) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // 日曜日
}

Methods

void close ()

关闭此格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

try (final var formatter = new Formatter(file.toFile())) {
    formatter.format("%s : %d", "abc", 1234);
}

System.out.println(Files.readString(file)); // abc : 1234
// An example without a try-with-resources statement.
final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

final var formatter = new Formatter(file.toFile());
try {
    formatter.format("%s : %d", "abc", 1234);
} finally {
    formatter.close();
}

System.out.println(Files.readString(file)); // abc : 1234

void flush ()

刷新此格式化程序。 (机器翻译)

final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

try (final var formatter = new Formatter(file.toFile())) {

    formatter.format("abc");
    System.out.println(Files.readString(file).isEmpty()); // true

    formatter.flush();
    System.out.println(Files.readString(file)); // abc

    formatter.format("XYZ");
    System.out.println(Files.readString(file)); // abc
}

System.out.println(Files.readString(file)); // abcXYZ

Formatter format (String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此对象的目标。 (机器翻译)

try (final var formatter = new Formatter()) {
    final var value = "abc";
    formatter.format("%s : %S", value, value);

    final var str = formatter.toString();
    System.out.println(str); // abc : ABC
}

try (final var formatter = new Formatter()) {
    final var value = 'x';
    formatter.format("%c : %C", value, value);

    final var str = formatter.toString();
    System.out.println(str); // x : X
}

try (final var formatter = new Formatter()) {
    final var value = 255;
    formatter.format("%d : %x : %X", value, value, value);

    final var str = formatter.toString();
    System.out.println(str); // 255 : ff : FF
}

try (final var formatter = new Formatter()) {
    final var value = 0.12345;
    formatter.format("%e : %f", value, value);

    final var str = formatter.toString();
    System.out.println(str); // 1.234500e-01 : 0.123450
}

Formatter format (Locale l, String format, Object... args)

使用指定的语言环境、格式字符串和参数将格式化的字符串写入此对象的目标。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

try (final var formatter = new Formatter()) {
    formatter.format("%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // Sunday
}

try (final var formatter = new Formatter()) {
    formatter.format(Locale.JAPAN, "%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // 日曜日
}

IOException ioException ()

返回此格式化程序的 Appendable 最后抛出的 IOException。 (机器翻译)

try (final var formatter = new Formatter()) {

    formatter.format("%s : %d", "abc", 1234);
    System.out.println(formatter); // abc : 1234

    final var ret = formatter.ioException();
    System.out.println(ret); // null
}
final var file = Path.of("R:", "java-work", "aaa.txt");
System.out.println(file); // R:\java-work\aaa.txt

try (final var out = Files.newOutputStream(file);
     final var formatter = new Formatter(out)) {

    // Intentionally closes the out object to make an error.
    out.close();

    formatter.format("%s : %d", "abc", 1234);
    formatter.flush();

    final var ret = formatter.ioException();
    System.out.println(ret); // java.nio.channels.ClosedChannelException
}

Locale locale ()

返回此格式化程序构造所设置的语言环境。 (机器翻译)

System.out.println(Locale.getDefault().toLanguageTag()); // en-US

try (final var formatter = new Formatter()) {
    System.out.println(formatter.locale().toLanguageTag()); // en-US

    formatter.format("%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // Sunday
}

try (final var formatter = new Formatter(Locale.JAPAN)) {
    System.out.println(formatter.locale().toLanguageTag()); // ja-JP

    formatter.format("%tA", DayOfWeek.SUNDAY);
    System.out.println(formatter); // 日曜日
}

Appendable out ()

返回输出的目的地。 (机器翻译)

try (final var formatter = new Formatter()) {
    final var out = formatter.out();
    System.out.println(out.getClass().getSimpleName()); // StringBuilder
}
try (final var formatter = new Formatter(new ByteArrayOutputStream())) {
    final var out = formatter.out();
    System.out.println(out.getClass().getSimpleName()); // BufferedWriter
}

String toString ()

返回在输出目标上调用 toString() 的结果。 (机器翻译)

try (final var formatter = new Formatter()) {
    final var value = "abc";
    formatter.format("%s : %S", value, value);

    final var str = formatter.toString();
    System.out.println(str); // abc : ABC
}

try (final var formatter = new Formatter()) {
    final var value = 'x';
    formatter.format("%c : %C", value, value);

    final var str = formatter.toString();
    System.out.println(str); // x : X
}

try (final var formatter = new Formatter()) {
    final var value = 255;
    formatter.format("%d : %x : %X", value, value, value);

    final var str = formatter.toString();
    System.out.println(str); // 255 : ff : FF
}

try (final var formatter = new Formatter()) {
    final var value = 0.12345;
    formatter.format("%e : %f", value, value);

    final var str = formatter.toString();
    System.out.println(str); // 1.234500e-01 : 0.123450
}

相关文章

To top of page