I have a specific issue with general console printing and I was wondering whether anyone has a solution for it. I am trying to print a dataTable which would look like sth like this:
Table
----------------------
Name |Surname |
----------------------
Mike |Mikhailowish|
Rafaello|Mirena |
and so on. In order to print the border of the bar I need to know what the maximum length of each column value is. I don’t want to go through the whole database to find that out and then again to print it. I would rather like to do sth like:
System.out.printLater(s); //herejust leave a pointer to a StringBuilder you will build
...
s.append("--------");
...
System.out.printAllDeferred();
I understand the above is probably in 99.99999% chances impossible, but perhaps you guys have a clever way of achieving the above?
You can wrap a
PrintStreamaround aByteArrayOutputStreamand then use that PrintStream instead ofSystem.out(or useSystem.setOut) to print to it. Later you can print the whole byte array instead.But that does not really help you since you’ll have to adjust all columns; better collect all cells from db into an array and calculate max lengths, then print the array.