Output in a table format in Java's System.out

前端 未结 8 1427
清歌不尽
清歌不尽 2020-11-22 15:03

I\'m getting results from a database and want to output the data as a table in Java\'s standard output

I\'ve tried using \\t but the first column I want is very vari

8条回答
  •  暖寄归人
    2020-11-22 15:47

    Because most of solutions is bit outdated I could also suggest asciitable which already available in maven (de.vandermeer:asciitable:0.3.2) and may produce very complicated configurations.

    Features (by offsite):

    • Text table with some flexibility for rules and content, alignment, format, padding, margins, and frames:
    • add text, as often as required in many different formats (string, text provider, render provider, ST, clusters),
    • removes all excessive white spaces (tabulators, extra blanks, combinations of carriage return and line feed),
    • 6 different text alignments: left, right, centered, justified, justified last line left, justified last line right,
    • flexible width, set for text and calculated in many different ways for rendering
    • padding characters for left and right padding (configurable separately)
    • padding characters for top and bottom padding (configurable separately)
    • several options for drawing grids
    • rules with different styles (as supported by the used grid theme: normal, light, strong, heavy)
    • top/bottom/left/right margins outside a frame
    • character conversion to generated text suitable for further process, e.g. for LaTeX and HTML

    And usage still looks easy:

    AsciiTable at = new AsciiTable();
    
    at.addRule();
    at.addRow("row 1 col 1", "row 1 col 2");
    at.addRule();
    at.addRow("row 2 col 1", "row 2 col 2");
    at.addRule();
    
    System.out.println(at.render()); // Finally, print the table to standard out.
    

提交回复
热议问题