Java打印表格 Console/控制台

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

效果图:

纯英文结果:
在这里插入图片描述

中英文混合结果:
在这里插入图片描述
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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值