使用 String.format java 以正确的方式对齐所有字符串

Align all the strings in proper way using String.format java

我正在创建一个文件,方法是附加 StringBuilder 中的所有字符串,然后将其转储到一个文件中。但是当我检查文件时(这很明显)我每行中的所有字符串都没有正确对齐..

下面是上层通过传递所有必要的东西调用的代码。

  private static StringBuilder appendStrings(String pro, List<ProcConfig> list,
      StringBuilder sb) {
    for (ProcConfig pc : list) {
      sb.append(pro).append(" ").append(pc.getNo()).append(" ").append(pc.getId())
          .append(" ").append(pc.getAddress()).append(" ").append(pc.getPort()).append(" ")
          .append(pc.getNumPorts()).append(" ").append(pc.getName())
          .append(System.getProperty("line.separator"));
    }
    sb.append(System.getProperty("line.separator"));
    return sb;
  }

下面是上面代码中的示例行。在每一行之后,都有一个新的集合,所以我想在每一行之前正确对齐每组中的所有行。

config 51 106 10.178.151.25 8095 5 tyt_87612nsas_woqa_7y2_0
config 51 104 10.124.192.124 8080 5 tyt_abc_pz1_rn03c-7vb_01

if_hello_abc_tree tyt.* then process_is_not_necessary 32 80 10.86.25.29 9091 5 tyt_goldenuserappslc22
if_hello_abc_tree tyt.* then process_is_not_necessary 51 50 10.174.192.209 9091 5 tyt_goldenuserapprno01

if_hello_abc_tree tyt.* then config 4 140 10.914.198.26 10001 1 silos_lvskafka-1702600
if_hello_abc_tree tyt.* then config 4 184 10.444.289.138 10001 1 silos_lvskafka-1887568

有什么方法可以正确对齐上面的每一行吗?所以输出应该是这样的:

config 51 106 10.178.151.25  8095 5 tyt_87612nsas_woqa_7y2_0
config 51 104 10.124.192.124 8080 5 tyt_abc_pz1_rn03c-7vb_01

if_hello_abc_tree tyt.* then process_is_not_necessary 32 80 10.86.25.29    9091 5 tyt_goldenuserappslc22
if_hello_abc_tree tyt.* then process_is_not_necessary 51 50 10.174.192.209 9091 5 tyt_goldenuserapprno01

if_hello_abc_tree tyt.* then config 4 140 10.914.198.26  10001 1 silos_lvskafka-1702600
if_hello_abc_tree tyt.* then config 4 184 10.444.289.138 10001 1 silos_lvskafka-1887568

更新:

下面是它现在是如何生成的。正如您在 IP 地址上看到的那样,如果您比较这两行,它会稍微偏离。

config 51 106   97.143.765.65 8095 5 abc_tyewaz1_rna03c-7nhl_02 
config 51 104  97.143.162.184 8080 5 abc_tyewaz1_rna03c-7vjb_01 

我们可以像下面这样生成吗?基本上使每一列笔直。这可能吗?

config 51 106  97.143.765.65  8095 5 abc_tyewaz1_rna03c-7nhl_02 
config 51 104  97.143.162.184 8080 5 abc_tyewaz1_rna03c-7vjb_01 

首先:我不是java开发者,但我略知一二。我知道这不是解决您问题的最佳方法,但您会明白这一点的。

不要多次调用 append 方法,而是使用 Formatter 并像这样循环它:

private static StringBuilder appendStrings(String pro, List<ProcConfig> list, StringBuilder sb) {
    Formatter formatter = new Formatter(sb);
    String template ="";
    for (ProcConfig pc : list) {
        if (pro.length() == 6)
            template = "%-6s %d %3d %-15s %d %d %s %n";
        else if (pro.length() > 35)
            template = "%-53s %d %3d %-15s %d %d %s %n";
        else
            template = "%-35s %d %3d %-15s %d %d %s %n";
        formatter.format(template, pro, pc.getNo(), pc.getId(), pc.getAddress(), pc.getPort(), pc.getNumPorts(), pc.getName());
    }
    formatter.close();
    return sb;
}

因为 pro 的长度不同,您可以将模板更改为适合每个 pro 的模板。

注意:不要忘记导入java.util.Formatter.

您可以使用格式说明符来指定数据的格式设置方式。这是常见格式化程序的列表:

%S or %s: Specifies String
%X or %x: Specifies hexadecimal integer
%o: Specifies Octal integer
%d: Specifies Decimal integer
%c: Specifies character
%T or %t: Specifies Time and date
%n: Inserts newline character
%B or %b: Specifies Boolean
%A or %a: Specifies floating point hexadecimal
%f: Specifies Decimal floating point

正如我之前在评论中所说,破折号表示第一个字符串左对齐,每个字符串都将附加在模板中定义的字符数之后,因此请尝试找到最适合您需要的数字。

要了解有关格式化程序的更多信息,请查看 here and here