有人知道将平面文件转换为 Java 对象的任何好的库吗?我发现了扁虫,但我正在寻找替代品。
问问题
20296 次
6 回答
9
快速更新:扁虫好久没活动了,有个fork叫BeanIO: http ://www.beanio.org/
于 2012-03-05T13:01:59.627 回答
4
FFP - 平面文件解析库
http://jffp.sourceforge.net/
于 2009-08-18T15:13:26.897 回答
2
我编写的另一个使用 Java 注释的替代方法是 JFileHelpers - http://jfilehelpers.com
带注释的 bean 的示例:
@FixedLengthRecord()
public class Customer {
@FieldFixedLength(4)
public Integer custId;
@FieldAlign(alignMode=AlignMode.Right)
@FieldFixedLength(20)
public String name;
@FieldFixedLength(3)
public Integer rating;
@FieldTrim(trimMode=TrimMode.Right)
@FieldFixedLength(10)
@FieldConverter(converter = ConverterKind.Date,
format = "dd-MM-yyyy")
public Date addedDate;
@FieldFixedLength(3)
@FieldOptional
public String stockSymbol;
}
那么你所要做的就是:
FileHelperEngine<Customer> engine =
new FileHelperEngine<Customer>(Customer.class);
List<Customer> customers =
new ArrayList<Customer>();
customers = engine.readResource(
"/samples/customers-fixed.txt");
于 2014-05-19T16:10:57.913 回答
1
您也可以尝试Fixedformat4j。我喜欢注释方法,并且定义自定义字段格式非常简单。
于 2010-03-02T08:11:04.320 回答
1
您想考虑JRecordBind(我是它的作者)
与其他人不同,它能够解析和创建平面文件,并且使用纯 XML 模式(因此您不必学习另一种配置语法)。一些用户回收相同的 XSD 来生成 web 服务和平面文件输出。
ps:我最近把代码移到了github
于 2012-11-28T08:39:44.187 回答