Continue parsing of records if exception occurs on some record in BeanIO

别说谁变了你拦得住时间么 提交于 2019-12-08 04:55:38

问题


I am using BeanIO to convert flat file into list of Object. Using following code snippet.

while ((MyCustomRecord obj = (MyCustomRecord) in.read()) != null) {
         System.out.println(obj);
}

But problem with this is, if exception occurs during parsing of some record, It immediately stops processing records. I want to suppress such exception and want it to continue processing of next records.

Is there a way to do this?

One thing that I can try is to put in.read in body of while block and wrap it around try/catch but then how would I detect end condition. There seems to be no method that tells end of records.

Thanks Jitendra


回答1:


Found answer in BeanIO reference documentation. I missed that earlier.

It can be done by registering your custom ErrorHandler in BeanReader.

in.setErrorHandler(new BeanReaderErrorHandlerSupport() {
            public void invalidRecord(InvalidRecordException ex) throws Exception {
                System.out.println(ex.getLocalizedMessage());
            } 
});

http://beanio.org/docs/reference/index.html#StreamValidation



来源:https://stackoverflow.com/questions/10238155/continue-parsing-of-records-if-exception-occurs-on-some-record-in-beanio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!