0

I am creating a file by appending all the strings in StringBuilder and then dumping it in a file. But when I check the file (which is obvious) all my strings in each line is not properly align..

Below is the code called by upper layer by passing all the necessary stuff.

  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;
  }

And here is how sample lines look like from the above code. After each new line, there is a new set so I want to align all the lines properly in each set before each new line.

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

Is there any way I can align each of the above line properly? So output should be like this:

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

Update:

Below is how it is getting generated now. As you can see on IP Address it is slightly off if you compare the two lines.

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 

Instead can we generate like below? Basically making each column straight. Is this possible to do?

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 
5
  • Append "\t" instead of empty string. Commented Sep 28, 2019 at 21:39
  • Is there any way to do this without using tabs? Commented Sep 28, 2019 at 21:48
  • Yes, You can use "%-5s%s%n" instead of tab. This way next string will be appended after 5 spaces or change it to whatever you need, The dash means the first string is left justified. Commented Sep 28, 2019 at 21:53
  • read this.. stackoverflow.com/questions/699878/… Commented Sep 28, 2019 at 21:58
  • @MasLoo how can I use this "%-5s%s%n" instead of tab or empty string in my string builder append method? I can't just replace this instead of tab because then it prints that as a string. So I need to plugin this with String.format or anything? Can you tell how I can use it? Commented Sep 28, 2019 at 22:07

1 Answer 1

1

First of all: I'm not a java developer, but I know a little about it. I know this is not the best way to solve your problem but you'll get the point.

Instead of calling append method multiple times use a Formatter and loop through it like this:

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;
}

Because pro is in different length you could change the template to a proper one for each pro.

Note: Don't forget to import java.util.Formatter.

You can use format specifiers to specify the way the data is formatted. This is a list of common formatters:

%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

As I said before in comments The dash means the first string is left justified and every string will append after number of chars defined in template, so try to find the best number that suites your needs.

To learn more about formatter take a look at here and here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your suggestion. It does work actually. I have updated my question with the output I get now from your suggestion. Can you see if we can make the output like that? I want all the values of a column straight in each line.
I've changed the %15s to %-15s in the answer. hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.