如何在控制台中使用ASCII创build表格?

我想组织这样的信息:

信息是由单元格组织的,而使用System.out.println信息会非常混乱。

或这个

尝试使用System.out.format()System.out.printf()printf只是调用format所以这两种方法给出相同的结果)。

这里有一个简单的例子,它将尝试将文本alignment到左侧,并用空格填充未使用的位置。 将string左alignment可以用%-15s实现,这意味着为string数据预留15位置,并从左( - )开始写入。 如果要添加数字,请使用d后缀(例如%-4d作为应置于列左侧的最多四位数字。
顺便说一句,我使用%n而不是\n来表示当前操作系统使用的行分隔符序列,就像Windows一样,它将是\r\n

您可以在Formatter类文档中find更多信息。

 String leftAlignFormat = "| %-15s | %-4d |%n"; System.out.format("+-----------------+------+%n"); System.out.format("| Column name | ID |%n"); System.out.format("+-----------------+------+%n"); for (int i = 0; i < 5; i++) { System.out.format(leftAlignFormat, "some data" + i, i * i); } System.out.format("+-----------------+------+%n"); 

产量

 +-----------------+------+ | Column name | ID | +-----------------+------+ | some data0 | 0 | | some data1 | 1 | | some data2 | 4 | | some data3 | 9 | | some data4 | 16 | +-----------------+------+ 

试试这个方法: asciitable 。

它提供了几个文本表的实现,最初使用ASCII和UTF-8字符作为边界。

这是一个示例表格:

  ┌───────────────────────────────────────────────── ─────────────────────────┐
     │表格标题│
     ├──────────────────┬──────────────────┬─────────── ───────┬─────────────────┤
     │第一行(col1)│有一些│甚至更多││
     ││信息│信息││
     ├──────────────────┼──────────────────┼─────────── ───────┼─────────────────┤
     │第二行│有一些│甚至更多││
     │(col1)│信息│信息││
     ││(col2)│(col3)││
     └──────────────────┴──────────────────┴─────────── ───────┴─────────────────┘ 

find最新版本: http //mvnrepository.com/artifact/de.vandermeer/asciitable

使用System.out.printf()

例如,

 String s = //Any string System.out.printf(%10s, s); 

将打印出String的内容,占用10个字符。 所以如果你想要一个表格,只要确保表格中的每个单元格打印出来的长度相同即可。 另外请注意, printf()不会打印新行,因此您必须自己打印。

你可以使用java-ascii-table 。 另见作者的网站 。

我专门为此创build的课程是完全dynamic的: https : //github.com/MRebhan/crogamp/blob/master/src/com/github/mrebhan/crogamp/cli/TableList.java

你可以像这样使用它:

 TableList tl = new TableList(3, "ID", "String 1", "String 2").sortBy(0).withUnicode(true); // from a list yourListOrWhatever.forEach(element -> tl.addRow(element.getID(), element.getS1(), element.getS2())); // or manually tl.addRow("Hi", "I am", "Bob"); tl.print(); 

它将看起来像这样的Unicode字符(注意:将在控制台看起来更好,因为所有的字符都是同样宽):

 ┌─────────┬─────────────────────────────────────────────────────────────────────────┬────────────────────────────┐ │ Command │ Description │ Syntax │ ┢━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╈━━━━━━━━━━━━━━━━━━━━━━━━━━━━┪ ┃ bye ┃ Quits the application. ┃ ┃ ┃ ga ┃ Adds the specified game. ┃ <id> <description> <path> ┃ ┃ gl ┃ Lists all currently added games ┃ [pattern] ┃ ┃ gr ┃ Rebuilds the files of the currently active game. ┃ ┃ ┃ gs ┃ Selects the specified game. ┃ <id> ┃ ┃ help ┃ Lists all available commands. ┃ [pattern] ┃ ┃ license ┃ Displays licensing info. ┃ ┃ ┃ ma ┃ Adds a mod to the currently active game. ┃ <id> <file> ┃ ┃ md ┃ Deletes the specified mod and removes all associated files. ┃ <id> ┃ ┃ me ┃ Toggles if the selected mod is active. ┃ <id> ┃ ┃ ml ┃ Lists all mods for the currently active game. ┃ [pattern] ┃ ┃ mm ┃ Moves the specified mod to the specified position in the priority list. ┃ <id> <position> ┃ ┃ top kek ┃ Test command. Do not use, may cause death and/or destruction ┃ ┃ ┃ ucode ┃ Toggles advanced unicode. (Enhanced characters) ┃ [on|true|yes|off|false|no] ┃ ┗━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ 

并与unicode字符(省略.withUnicode(真)):

 Command | Description | Syntax --------+-------------------------------------------------------------------------+--------------------------- bye | Quits the application. | ga | Adds the specified game. | <id> <description> <path> gl | Lists all currently added games | [pattern] gr | Rebuilds the files of the currently active game. | gs | Selects the specified game. | <id> help | Lists all available commands. | [pattern] license | Displays licensing info. | ma | Adds a mod to the currently active game. | <id> <file> md | Deletes the specified mod and removes all associated files. | <id> me | Toggles if the selected mod is active. | <id> ml | Lists all mods for the currently active game. | [pattern] mm | Moves the specified mod to the specified position in the priority list. | <id> <position> top kek | Test command. Do not use, may cause death and/or destruction | ucode | Toggles advanced unicode. (Enhanced characters) | [on|true|yes|off|false|no] 

这也很好用http://sourceforge.net/projects/texttablefmt/ 。 Apache也许可。

你可以使用正确的方法使用string.format()代码可以看起来像这样我猜

 StringBuilder sb=new StringBuilder(); for(int i = 1; i <= numberOfColumns; i++) { sb.append(String.format(%-10s,rsMetaData.getColumnLabel(i); } 

至于图书馆我不认为有任何工作会做,但我可能会误会! 实际上会研究它

另请参阅http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

TUIAWT允许您在控制台窗口中使用AWT组件。 它看起来不像支持List或Table ,但它可能会给你一个起点。