我真的相信@JoniVR 的建议会很有帮助,您应该考虑为每行的列使用分隔符。目前,您将无法解析像名字“Mary Ann”这样的复合数据。此外,由于您提供的示例数据已经有 4 行,您应该有一个 POJO 来表示从文件中解析的数据。一个概念性的看起来像:
class MyPojo {
private String name;
private int postCode;
private String state;
private int cityId;
public MyPojo(String name, int postCode, String state, int cityId) {
this.name = name;
this.postCode = postCode;
this.state = state;
this.cityId = cityId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPostCode() {
return postCode;
}
public void setPostCode(int postCode) {
this.postCode = postCode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
@Override
public String toString() {
return "MyPojo{" +
"name='" + name + '\'' +
", postCode=" + postCode +
", state='" + state + '\'' +
", cityId=" + cityId +
'}';
}
}
然后,您希望在验证我猜的行之后遇到错误,因此最好考虑某种错误类来存储这些错误(可能是一个设计合理的扩展异常类的类?)。为此目的,一个非常简单的类是:
class InsertionError {
private String message;
private int lineNumber;
public InsertionError(String message, int lineNumber) {
this.message = message;
this.lineNumber = lineNumber;
}
@Override
public String toString() {
return "Error at line " + lineNumber + " -> " + message;
}
}
然后解决方案本身应该:
1. 拆分行。
2. 标记每行的列并解析/验证它们。
3. 以有用的 java 表示形式收集列数据。
也许是这样的:
private static final int HEADERS_COUNT = 4;
private static final int LINE_NUMBER_CURSOR = 0;
public static void main(String[] args) {
String data = "Marsha 1234 Florida 1268\n" +
"Jasmine Texas 4456\n" +
"Jane 1523 Texas 4456\n" +
"Jasmine Texas 2233 asd\n" +
"Mark 7253 Georgia 1234";
int[] lineNumber = new int[1];
List<InsertionError> errors = new ArrayList<>();
List<MyPojo> insertedPojo = Arrays.stream(data.split("\n"))
.map(x -> x.split("\\p{Blank}+"))
.map(x -> {
lineNumber[LINE_NUMBER_CURSOR]++;
if (x.length == HEADERS_COUNT) {
Integer postCode = null;
Integer cityId = null;
try {
postCode = Integer.valueOf(x[1]);
} catch (NumberFormatException ignored) {
errors.add(new InsertionError("\"" + x[1] + "\" is not a numeric value.", lineNumber[LINE_NUMBER_CURSOR]));
}
try {
cityId = Integer.valueOf(x[3]);
} catch (NumberFormatException ignored) {
errors.add(new InsertionError("\"" + x[3] + "\" is not a numeric value.", lineNumber[LINE_NUMBER_CURSOR]));
}
if (postCode != null && cityId != null) {
return new MyPojo(x[0], postCode, x[2], cityId);
}
} else {
errors.add(new InsertionError("Columns count does not match headers count.", lineNumber[LINE_NUMBER_CURSOR]));
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
errors.forEach(System.out::println);
System.out.println("Number of successfully inserted Pojos is " + insertedPojo.size() + ". Respectively they are: ");
insertedPojo.forEach(System.out::println);
}
,打印:
第 2 行出错 -> 列数与标题数不匹配。
第 4 行出错 -> “Texas”不是数值。
第 4 行出错 -> “asd”不是数值。
成功插入 Pojo 的数量为 3。分别是:
MyPojo{name='Marsha', postCode=1234, state='Florida', cityId=1268}
MyPojo{name='Jane', postCode=1523, state='Texas ', cityId=4456}
MyPojo{name='Mark', postCode=7253, state='Georgia', cityId=1234}