fix:寫入穩定格式化處理優化

This commit is contained in:
xiaoguangbin 2023-09-27 15:10:26 +08:00
parent 59cc54cf24
commit fba4ac0915

View File

@ -127,7 +127,7 @@ public class MyLogFormatUtil {
List<String> channels = Arrays.asList("59.541", "88.034", "122.061", "165.857", "391.698", "661.657", "834.838", "898.036", "1115.540", "1173.230", "1332.490", "1836.050");
List<String> energies = Arrays.asList("0.168", "0.176", "0.174", "0.155", "0.092", "0.059", "0.051", "0.040", "0.040", "0.031", "0.028", "0.022");
List<String> errors = Arrays.asList("0.003", "0.004", "0.003", "0.003", "0.002", "0.001", "0.001", "0.001", "0.001", "0.001", "0.001", "0.000");
List<String> errors = Arrays.asList("0.003", "0.004", "0.003", "0.003", "0.002", "0.001", "0.001", "0.001", "0.001", "0.001", "0.001");
List<AttributeItemVo> energys = new ArrayList<>();
AttributeItemVo attributeItemVo = new AttributeItemVo();
@ -142,7 +142,7 @@ public class MyLogFormatUtil {
attributeItemVo.setAttribute("Error");
attributeItemVo.setContext(errors);
energys.add(attributeItemVo);
// MyLogFormatUtil.getBlock(MyLogFormatUtil.SetSampleGEnergyChannel, "sampleId", energys);
MyLogFormatUtil.getBlock(MyLogFormatUtil.SetSampleGEnergyChannel, "sampleId", energys);
Console.log(getTitleFormat("Read calibration finished","."));
@ -294,25 +294,21 @@ public class MyLogFormatUtil {
return result;
}
// 计算每列数据中最长的字符串长度
StringBuilder sb = new StringBuilder();
int[] columnWidths = new int[data.get(0).size()];
for (List<String> row : data) {
for (int i = 0; i < row.size(); i++) {
// 获取长度
columnWidths[i] = Math.max(columnWidths[i], row.get(i).length());
// 构造格式化字符串
sb.append("%-").append(columnWidths[i] + 4).append("s");
}
// 构造格式化字符串
result.add(String.format(sb.toString(), row.toArray()));
result.add(STRING_END);
Console.log(String.format(sb.toString(), row.toArray()));
sb = new StringBuilder();
}
// 构造格式化字符串
StringBuilder formatBuilder = new StringBuilder();
for (int i = 0; i < columnWidths.length; i++) {
formatBuilder.append("%-").append(columnWidths[i] + 4).append("s");
}
String format = formatBuilder.toString();
// 格式化输出日志
for (List<String> row : data) {
result.add(String.format(format, row.toArray()));
Console.log(String.format(format, row.toArray()));
}
result.add(STRING_END);
return result;
}