import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* @Description TODO
* @Date 2023/1/4 10:14
* @Author Shixy
*/
@Slf4j
@Getter
public class EpidataUtils {
private List<String> lines;
private StringBuilder line;
private static final int LINE_MAX_LENGTH = 78;
private static final String CHINESE_REGEX = "[^\\x00-\\xff]+";
private static final String SPACE = " ";
private static final String END_SYMBOL = "!";
private static final String GBK = "GBK";
private EpidataUtils() {}
public static EpidataUtils getInstance() {
EpidataUtils epidataUtils = new EpidataUtils();
epidataUtils.lines = new ArrayList<>();
epidataUtils.line = new StringBuilder();
return epidataUtils;
}
public void append(String str) {
try {
int lineLength = line.toString().getBytes(GBK).length;
int strLength = str.getBytes(GBK).length;
if (lineLength + strLength > LINE_MAX_LENGTH) {
line.append(str, 0, LINE_MAX_LENGTH - lineLength);
line.append(END_SYMBOL);
lines.add(line.toString());
line = new StringBuilder();
line.append(str, LINE_MAX_LENGTH - lineLength, str.length());
} else if (lineLength + strLength == LINE_MAX_LENGTH) {
line.append(str);
line.append(END_SYMBOL);
lines.add(line.toString());
line = new StringBuilder();
} else {
line.append(str);
}
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码: ", e);
} catch (IndexOutOfBoundsException e) {
log.error("字符截取出错: ", e);
}
}
public void appendFinish() {
line.append(END_SYMBOL);
lines.add(line.toString());
}
public static String appendSpace(String content, int length) {
try {
Boolean flag = Boolean.FALSE;
int contentLength = content.getBytes(GBK).length;
StringBuilder sb = new StringBuilder();
if (contentLength > length) {
throw new IndexOutOfBoundsException("index " + contentLength + ", max length " + length);
}
if (content.trim().matches(CHINESE_REGEX)) {
sb.append(content);
flag = Boolean.TRUE;
}
for (int i = 0; i < length - contentLength; i++) {
sb.append(SPACE);
}
if (!flag) {
sb.append(content);
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码: ", e);
return "";
}
}
public static void main(String[] args) {
Map<String, String> map = new LinkedHashMap<>();
map.put("ID1", "xxxxxxxxxxxxxxxx");
map.put("province", "44");
map.put("city", "15");
map.put("district", "2");
map.put("county", "02");
map.put("point", "2");
map.put("school", "06");
map.put("grade", "53");
map.put("num", "0101");
map.put("ID2", "xxxxxxxxxxxxxxxx");
map.put("gender", "1");
map.put("nation", "1");
map.put("nationothe", " ");
map.put("birth", "2015/09/04");
map.put("examine", "2020/11/12");
map.put("glasstype", "4");
map.put("OKR", " ");
map.put("OKL", " ");
map.put("visionR", "5.0");
map.put("glassR", " ");
map.put("visionL", "5.0");
map.put("glassL", " ");
map.put("name2", "王小二 ");
map.put("spherR", " 0.50");
map.put("cylinR", " -0.25");
map.put("axisR", "170");
map.put("SER", " 0.375");
map.put("spherRT", " ");
map.put("cylinRT", " ");
map.put("axisRT", " ");
map.put("spherL", " 0.25");
map.put("cylinL", " -0.25");
map.put("axisL", " 10");
map.put("SEL", " 0.125");
map.put("spherLT", " ");
map.put("cylinLT", " ");
map.put("axisLT", " ");
map.put("NOTE", " ");
map.put("name", "王小二 ");
map.put("date", "2020/11/12");
EpidataUtils instance = getInstance();
map.forEach((k, v) -> {
instance.append(v);
});
instance.appendFinish();
List<String> lines1 = instance.getLines();
lines1.forEach(System.out::println);
}
}
标签:map,Epidata,length,构建,put,new,工具,line,append
From: https://www.cnblogs.com/Smile-yun-1996/p/17025096.html