有这么一个txt文件
[HEAD]
Batch:=130438
CardType:=100
CardTheme:=充值付费卡
CardNumber:=250
StartCardNo:=0211001304380000001
EndCardNo:=0211001304380000250
StartDate:=20130410
StopDate:=20180630
Reservestopdate:=20190630
Facevalue:=500
[BEGIN]
0211001304380000001 XXXXXXXXXXXXXXXXXX
0211001304380000002 XXXXXXXXXXXXXXXXXX
0211001304380000003 XXXXXXXXXXXXXXXXXX
[END]
有两个类 一个是充值批次 一个是充值明细 关系式一对多
[BEGIN]与[END]中间是明细 可以有多条 我要把[HEAD]与[BEGIN]set到充值批次中
把[BEGIN]也[END]中间的值set到充值明细中
实体类如下
public class RechargeBatch extends BaseEntity {
private String batch; //批次编号
private String cardType; //充值卡类型编号
private String cardTheme; //充值卡类型说明
private Integer cardNumber; //充值卡数量
private String cardNoStart; //充值卡开始卡号
private String cardNoEnd; //充值卡结束卡号
private Date startDate; //充值卡开始使用日期
private Date stopDate; //充值卡结束使用日期
private Date reserveStopDate; //充值卡最终保留结束使用日期
private Integer faceValue; //充值卡面值
private Set<RechargeBatchInfo> rechargeBatchInfoSet; //充值卡明细项
//省略getset方法
}
public class RechargeBatchInfo extends BaseEntity {
private String cardNo; //充值卡号
private String cardPwd; //充值卡密
private String cardStatus; //充值卡状态
private RechargeBatch rechargeBatch; //充值卡批次
//省略getset方法
}
数据流 String
------解决方案--------------------
package com.tur.demo;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Hello {
public static void main(String[] args) {
RechargeBatch batch = null;
Set<RechargeBatchInfo> batchInfos = new HashSet<RechargeBatchInfo>();
Scanner scanner = new Scanner(ClassLoader.getSystemClassLoader().getResourceAsStream("data.txt"));
Pattern batchPattern = Pattern.compile("^(.+):=(.+)$");
Map<String, String> batchFields = new HashMap<String, String>();
while (scanner.hasNext()) {
String str = scanner.nextLine();
if (str.matches("^(.+):=(.+)$")) {
Matcher matcher = batchPattern.matcher(str);
while (matcher.find()) {
batchFields.put(matcher.group(1), matcher.group(2));