0

我正在尝试读取固定长度的文件。我想提取文档列表,但仅使用具有字段值“BB”的 RecordHeader (RH) 进行过滤。因此,使用 recordHeader 跳过 Document 等于:

RH20210607AA   

这是我的课堂文档的一个例子

@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;
}

这是一个固定长度文件的例子:

fake
Unknown record
Unknown record
RH20210607AA    
RDitem1
RDitem2
Unknown record
RH20210607BB    
RDitem2
RDitem3
Unknown record
Unknown record
Unknown record
RH20210607BB    
RDitem1
RDitem4
RDitem5

感谢帮助

4

1 回答 1

0

您可以在班级中使用多个字段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;

}

@Field注意字段注释上的额外属性documentCode-->rid = true, literal = "BB"

literal = "BB"如果您需要包含更多RH记录或将其用作排除过滤器,则可以使用正则表达式代替。

您可能已经这样做了,您还需要配置StreamFactory忽略无法识别的记录:

final StreamBuilder builder = new StreamBuilder("streamName")
    .readOnly()
    .format("fixedlength")
    .parser(new FixedLengthParserBuilder())
    .ignoreUnidentifiedRecords()             // <-- important
    .addGroup(...)
    .addRecord(RD.class));
factory.define(builder);
于 2021-09-29T19:43:06.063 回答