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
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.