Java输出文字表格(更新V2)
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* 网格化String输出
* <p>
* File: StringGrid
* Create: 2018/11/26
* Modify: 2018/11/26
* Version: 1.0.0
* JDK: 8
*/
public class StringGrid {
private static final int COLUMN_SPACE = 2;
private ArrayList<String[]> contentList = new ArrayList<>();
private Map<Integer, Integer> widthMap = new HashMap<>();
public static void main(String[] args) throws UnsupportedEncodingException {
StringGrid grid = new StringGrid();
grid.add("1 2 3 4");
grid.add("1111 2aaa 3bbb 555sdfdsf");
grid.add("春 2aaa 3bbb 555sdfdsf");
grid.add(new Object[]{777, 2343242, 121312, "111"});
System.out.println(grid.toString());
}
public void add(String str) throws UnsupportedEncodingException {
add(str.split(" "));
}
public void add(Object[] args) throws UnsupportedEncodingException {
for (int i = 0; i < args.length; i++) {
Integer width = widthMap.get(i);
if (width == null) width = 0;
if (args[i].toString().getBytes("GBK").length > width) {
widthMap.put(i, args[i].toString().getBytes("GBK").length);
}
}
final String[] strings = new String[args.length];
for (int i = 0; i < args.length; i++) {
strings[i] = args[i] + "";
}
contentList.add(strings);
}
public String toString() {
String result = "";
// make result string
result += lines() + "\n";
for (String[] strings : contentList) {
result += "|";
for (int i = 0; i < strings.length; i++) {
try {
if(widthMap.get(i) > 40)
result += strings[i] + columnPaddingLeft(strings[i], widthMap.get(i)) + columnPaddingRight(strings[i], widthMap.get(i)) + "|";
else
result += columnPaddingLeft(strings[i], widthMap.get(i)) + strings[i] + columnPaddingRight(strings[i], widthMap.get(i)) + "|";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
result += "\n" + lines() + "\n";
}
return result;
}
private String lines() {
String s = "+";
for (Integer integer : widthMap.values()) {
s += padding(integer + COLUMN_SPACE * 2, "-") + "+";
}
return s;
}
private String columnPaddingLeft(String string, int columnWidth) throws UnsupportedEncodingException {
int diff = columnWidth - string.getBytes("GBK").length;
// 奇数左边少一个空格
if (diff % 2 == 1) diff -= 1;
return padding(diff / 2 + COLUMN_SPACE, " ");
}
private String columnPaddingRight(String string, int columnWidth) throws UnsupportedEncodingException {
int diff = columnWidth - string.getBytes("GBK").length;
// 奇数右边多一个空格
if (diff % 2 == 1) diff += 1;
return padding(diff / 2 + COLUMN_SPACE, " ");
}
/**
* 填充字符
*
* @param length 长度
* @param pad 字符
* @return 固定长度的字符
*/
private String padding(int length, String pad) {
String s = "";
for (int i = 0; i < length; i++) {
s += pad;
}
return s;
}
}
V2 新增了单行输出
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class StringGrid {
private static final int COLUMN_SPACE = 2;
private static final String SINGLE_LINE_MARK = "___";
private ArrayList<String[]> contentList = new ArrayList<>();
private Map<Integer, Integer> widthMap = new HashMap<>();
public static void main(String[] args) throws UnsupportedEncodingException {
StringGrid grid = new StringGrid();
grid.add("1111 2aaa 3bbb 555sdfdsf");
grid.add("春 2aaa 3bbb 555sdfdsf");
grid.add(new Object[]{777, 2343242, 121312, "111"});
grid.add("___ 1 2 3 4 5 蠢 呵呵呵呵呵呵呵呵呵呵呵呵 h");
System.out.println(grid.toString());
}
public void add(String str) throws UnsupportedEncodingException {
add(str.split(" "));
}
public void add(Object[] args) throws UnsupportedEncodingException {
// 计算宽度
if (args.length > 0 && !args[0].toString().startsWith(SINGLE_LINE_MARK)) {
for (int i = 0; i < args.length; i++) {
Integer width = widthMap.get(i);
if (width == null) width = 0;
if (args[i].toString().getBytes("GBK").length > width) {
widthMap.put(i, args[i].toString().getBytes("GBK").length);
}
}
}
// 分拆字段
final String[] strings = new String[args.length];
for (int i = 0; i < args.length; i++) {
strings[i] = args[i] + "";
}
contentList.add(strings);
}
public String toString() {
StringBuilder result = new StringBuilder();
// make result string
result.append(lines()).append("\n");
for (String[] strings : contentList) {
result.append("|");
// @TODO 有BUG
if (strings.length > 0 && strings[0].startsWith(SINGLE_LINE_MARK)) {
// 单行记录
StringBuilder lineText = new StringBuilder();
for (String string : strings) {
lineText.append(string).append(" ");
}
String line = lineText.toString().replace(SINGLE_LINE_MARK, "");
result.append(line).append(columnPaddingLeft(line, totalWidth() - 4)).append(columnPaddingRight(line, totalWidth() - 4)).append("|");
} else
for (int i = 0; i < strings.length; i++) {
if (widthMap.get(i) > 40)
result.append(strings[i]).append(columnPaddingLeft(strings[i], widthMap.get(i))).append(columnPaddingRight(strings[i], widthMap.get(i))).append("|");
else
result.append(columnPaddingLeft(strings[i], widthMap.get(i))).append(strings[i]).append(columnPaddingRight(strings[i], widthMap.get(i))).append("|");
}
result.append("\n").append(lines()).append("\n");
}
return result.toString();
}
/**
* @return 横线
*/
private String lines() {
StringBuilder s = new StringBuilder("+");
for (Integer integer : widthMap.values()) {
s.append(padding(integer + COLUMN_SPACE * 2, "-")).append("+");
}
return s.toString();
}
/**
* @return 总宽度
*/
private int totalWidth() {
int length = 0;
for (Integer value : widthMap.values()) {
length += (value + COLUMN_SPACE * 2 + 1);
}
return length - 1;
}
private String columnPaddingLeft(String string, int columnWidth) {
int diff = 0;
try {
diff = columnWidth - string.getBytes("GBK").length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 奇数左边少一个空格
if (diff % 2 == 1) diff -= 1;
return padding(diff / 2 + COLUMN_SPACE, " ");
}
private String columnPaddingRight(String string, int columnWidth) {
int diff = 0;
try {
diff = columnWidth - string.getBytes("GBK").length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 奇数右边多一个空格
if (diff % 2 == 1) diff += 1;
return padding(diff / 2 + COLUMN_SPACE, " ");
}
/**
* 填充字符
*
* @param length 长度
* @param pad 字符
* @return 固定长度的字符
*/
private String padding(int length, String pad) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < length; i++) {
s.append(pad);
}
return s.toString();
}
}
V2输出效果:
+--------+-----------+----------+-------------+
| 1111 | 2aaa | 3bbb | 555sdfdsf |
+--------+-----------+----------+-------------+
| 春 | 2aaa | 3bbb | 555sdfdsf |
+--------+-----------+----------+-------------+
| 777 | 2343242 | 121312 | 111 |
+--------+-----------+----------+-------------+
| 1 2 3 4 5 蠢 呵呵呵呵呵呵呵呵呵呵呵呵 h |
+--------+-----------+----------+-------------+