我正在尝试读取固定长度的文件。我想提取文档列表,但过滤只有RecordHeader (RH)字段值"BB"所以跳过Document with recordHeader =:
RH20210607AA
这是我的类Document
的例子@Group
public class Document{
@Record(order = 1, minOccurs=1, maxOccurs=1)
private RH recordHeader;
@Record(order = 2, minOccurs=1, maxOccurs = -1, type=RD.class, collection = List.class)
private List<RD> recordDetails;
}
这是我的类RH
的一个例子@Record
public class RH{
@Field(ordinal = 0, required = true, length = 2, align = Align.LEFT, rid = true, literal = "RH")
private String recordType;
@Field(ordinal = 1, required = true, length=8, format = "yyyyMMdd")
private LocalDate documentDate;
@Field(ordinal = 2, required = true, length = 2, padding = ' ', align = Align.LEFT)
private String documentCode;
}
这是一个fixedLength文件的例子:
fake
Unknown record
Unknown record
RH20210607AA
RDitem1
RDitem2
Unknown record
RH20210607BB
RDitem2
RDitem3
Unknown record
Unknown record
Unknown record
RH20210607BB
RDitem1
RDitem4
RDitem5
Thanks for help
您可以在RH
类中使用多个字段来标识您想要的记录。你可以这样做:
@Record
public class RH {
@Field(ordinal = 0, required = true, length = 2, align = Align.LEFT, rid = true, literal = "RH")
private String recordType;
@Field(ordinal = 1, required = true, length = 8, format = "yyyyMMdd")
private Date documentDate;
@Field(ordinal = 2, required = true, length = 2, padding = ' ', align = Align.LEFT, rid = true, literal = "BB")
private String documentCode;
}
注意documentCode
字段的@Field
注释上的额外属性——>rid = true, literal = "BB"
如果需要包含更多的RH
记录,可以使用正则表达式代替literal = "BB"
,或者将其用作排除过滤器。
您可能已经这样做了,您还需要配置StreamFactory
以忽略不可识别的记录:
final StreamBuilder builder = new StreamBuilder("streamName")
.readOnly()
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.ignoreUnidentifiedRecords() // <-- important
.addGroup(...)
.addRecord(RD.class));
factory.define(builder);