如何使用beanIO解析固定长度的记录?

时间:2015-02-23 15:11:50

标签: java bean-io

我使用beanio来解析固定长度的内容(单个字符串行)。我想使用基于注释的配置,但它不起作用。

以下代码可能出现什么问题?

        StreamBuilder builder = new StreamBuilder("s1").addRecord(MyRecord.class);
        StreamFactory factory = StreamFactory.newInstance();
        factory.define(builder);

        Unmarshaller unmarshaller = factory.createUnmarshaller("s1");
        unmarshaller.unmarshal("123ASD");

@Record(minOccurs = 1, maxOccurs = 1)
public class MyRecord {

    @Field(at = 1, length = 3, minOccurs = 1)
    private String number;

    @Field(at = 4, length = 3, minOccurs = 1)   
    private String text;
}


org.beanio.InvalidRecordException: Invalid 'myRecord' record
 ==> Invalid 'number':  Expected minimum 1 occurrences
 ==> Invalid 'text':  Expected minimum 1 occurrences
    at org.beanio.internal.parser.UnmarshallingContext.validate(UnmarshallingContext.java:200)
    at org.beanio.internal.parser.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:204)
    at org.beanio.internal.parser.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:89)

1 个答案:

答案 0 :(得分:6)

我错过了以下内容:

new StreamBuilder("s1").format("fixedlength").addRecord(MyRecord.class);