700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java打印表格 Console/控制台

Java打印表格 Console/控制台

时间:2024-03-06 03:33:43

相关推荐

Java打印表格 Console/控制台

功能: 控制台打印表格,支持字段动态长度,左对齐,设置最多打印多少行。

类下载地址:/download/qq_26599807/12840079简单使用方法:传入表头和内容即可,如例1。

效果图:

纯英文结果:

中英文混合结果:

SQL查询结果:

例1:简单使用

ConsoleTable t = new ConsoleTable();t.appendHeader("No").appendHeader("Name").appendHeader("Sex").appendHeader("Undifin");t.appendRow();t.appendBody("S01").appendBody("MrLiu").appendBody("O").appendBody("88");t.appendRow();t.appendBody("S02").appendBody("MrLiu").appendBody("O").appendBody("99");t.appendRow();t.appendBody("S03").appendBody("MrLiu").appendBody("X").appendBody("58");System.out.println(t.toString());

ConsoleTable t = new ConsoleTable();t.appendHeader("学号").appendHeader("姓名").appendHeader("性别").appendHeader("成绩");t.appendRow();t.appendBody("S01").appendBody("刘亿菲").appendBody("女").appendBody("88");t.appendRow();t.appendBody("中开04").appendBody("a").appendBody("b").appendBody("7");t.appendRow();t.appendBody("S02").appendBody("迪丽乐巴").appendBody("女").appendBody("99");t.appendRow();t.appendBody("S03").appendBody("张伟").appendBody("男").appendBody("58");System.out.println(t.toString());

例2:进阶使用

数据类型:Mysql的desc表结构的结果,List结果,show index结果等等…

进阶输出:调用toString方法,得到字符串,可以保存到日志文件。

部分代码:

private List<Object> header = new ArrayList<Object>();private int headerSize = 0;private List<List<Object>> body = new ArrayList<List<Object>>();private int bodySize = 0;private boolean printHeader = true;private int maxValueLenth = 50;public boolean isPrintHeader() {return printHeader;}public void setPrintHeader(boolean printHeader) {this.printHeader = printHeader;}public int getMaxValueLenth() {return maxValueLenth;}/*** TODO 设置列值最大长度,超过则用省略号代替(需大于0且在头与内容前面设置有效)* * @param maxValueLenth*/public void setMaxValueLenth(int maxValueLenth) {if (maxValueLenth > 0 && headerSize + bodySize == 0)this.maxValueLenth = maxValueLenth;}public ConsoleTable() {}public ConsoleTable(boolean printHeader) {this.printHeader = printHeader;}public ConsoleTable(boolean printHeader, int maxValueLenth) {this.printHeader = printHeader;this.maxValueLenth = maxValueLenth;}

缺点:由于使用了\0字符串结尾字符,不支持复制粘贴。

更多代码请参考具体类,下载链接:/download/qq_26599807/12840079

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。