在 BeanIO 的配置中可以设置 BeanReader 的 strict 匹配模式来处理空文件或没有记录存在的情况。当 strict 属性设置为 true 时,BeanReader 在读取文件时会抛出异常,如果设置为 false,则不会抛出异常。
代码示例:
// 创建配置
StreamFactory factory = StreamFactory.newInstance();
// 设置 strict 模式为 false
factory.getStreamDefinition("myRecord").setStrict(false);
// 从文件读取记录
BeanReader in = factory.createReader("myFile", new File("path/to/my/file"));
MyRecord record = null;
while ((record = (MyRecord) in.read()) != null) {
// 处理记录
}
in.close();