功能: 控制台打印表格,支持字段动态长度,左对齐,设置最多打印多少行。
- 类下载地址:https://download.csdn.net/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字符串结尾字符,不支持复制粘贴。
更多代码请参考具体类,下载链接:https://download.csdn.net/download/qq_26599807/12840079