因为百度半天都在那互相复制,有的自己都没测过就直接复制粘贴
没有处理代码之前:
代码:
//先获取到要处理的字段
String s1 = tblDconlSettleDtlTmp.getCouponNm().trim();
System.out.println(s1.length());
//20是设置的格式长度
//打印字符串中有多少数字,英文
int szCount = 0;
int zmCount = 0;
String s1 = sf0001rptTmp.getOrgNm().trim();
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if ((c >= '0' && c <= '9') || (c >= 32 && c <= 47) || (c >= 58 && c <= 64)
|| (c >= 91 && c <= 96) || (c >= 123 && c <= 127))
szCount++;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
zmCount++;
}
int format = 20 - s1.length() + szCount + zmCount;
//手动拼写格式刷
String a = "%-"+format +"s";
String line1 = String.format("%-12s"+a+"%-12s% \r\n",
tblDconlSettleDtlTmp.getSrvId(),
tblDconlSettleDtlTmp.getCouponNm().trim(), //中文字符串
tblDconlSettleDtlTmp.getCouponCode()
);
fileWriterSrv.write(line1);
//格式化字符串 对齐
public String format(String s1) {
int szCount = 0;
int zmCount = 0;
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if ((c >= '0' && c <= '9') || (c >= 32 && c <= 47) || (c >= 58 && c <= 64)
|| (c >= 91 && c <= 96) || (c >= 123 && c <= 127))
szCount++;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
zmCount++;
}
int format = 20 - s1.length() + szCount + zmCount;
String f = "%-" + format + "s";
return f;
}
处理代码之后:
这就好了 至于format详细用法还不是很了解 记录一下