StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String rowFormat = "%8s %8s %8s %8s%n";
pw.printf(rowFormat, "Col A", "Col B", "Col C", "Col XY");
pw.printf(rowFormat, "A", "19", "Car", "55");
pw.printf(rowFormat, "X", "21", "Train C", "-4");
pw.printf(rowFormat, "B", "-9", "Bike", "0");
String message = sw.toString();
System.out.println(message);
The above program prints the following table and the alignment is perfect.
Col A Col B Col C Col XY
A 19 Car 55
X 21 Train C -4
B -9 Bike 0
Instead of printing the string message
, I am passing the string to another function which sends email with the content of the mail as the string message
.
mail.setContent(message.toString(),"text/plain");
This is how I am setting the content as message in my mail.
But the alignment of the table is not appropriate as I do receive while printing the string. What must be done such that I receive the content of my mail as similar as the printing format of the string. Please advise on this regard.