将平面文件转换为 Java 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1294405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Flat File to Java Objects
提问by mkoryak
采纳答案by Robert Harvey
FFP - Flat file parsing library
http://jffp.sourceforge.net/
FFP - 平面文件解析库
http://jffp.sourceforge.net/
回答by Виталий Олегович
Quick update: flatworm has not been active for quite a while, there is a fork named BeanIO: http://www.beanio.org/
快速更新:扁虫已经有一段时间没有活跃了,有一个名为BeanIO的分支:http: //www.beanio.org/
回答by Anver Sadhat
I have not used this JFlat, But it seems this Framework provides conversion from Flat file to Java object.
我没有使用过这个JFlat,但是这个框架似乎提供了从平面文件到 Java 对象的转换。
Similarly BeanIOand Jsefaalso provides a simple and flexible API.
You can try with FlatPack- but it is OLD and the docs are not good as JFlat or BeanIO
您可以尝试使用FlatPack- 但它是旧的并且文档不如 JFlat 或 BeanIO
Apache Camel has Flatpack componentas well as from 2.10 it has BeanIO component
Apache Camel 具有Flatpack 组件以及从 2.10 开始它具有BeanIO 组件
回答by kolrie
Another alternative, that I wrote that uses Java Annotations is JFileHelpers - http://jfilehelpers.com
另一种使用 Java 注释的替代方法是 JFileHelpers - http://jfilehelpers.com
An example of annotated bean:
一个带注释的 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;
}
Then all you have to do is:
那么你所要做的就是:
FileHelperEngine<Customer> engine =
new FileHelperEngine<Customer>(Customer.class);
List<Customer> customers =
new ArrayList<Customer>();
customers = engine.readResource(
"/samples/customers-fixed.txt");
回答by Bartosz Bierkowski
You can also give a try to Fixedformat4j. I like the annotations approach and it's very simple to define a custom field format.
您也可以尝试Fixedformat4j。我喜欢注释方法,定义自定义字段格式非常简单。
回答by Federico Fissore
You would like to consider JRecordBind(I'm its author)
你想考虑JRecordBind(我是它的作者)
Unlike others, it's able to both parse and create flat files and it uses plain XML Schema (so you don't have to learn yet another configuration syntax). Some users recycle the same XSD for producing both webservice and flat files output.
与其他人不同的是,它能够解析和创建平面文件,并且使用纯 XML 模式(因此您不必再学习另一种配置语法)。一些用户回收相同的 XSD 来生成 web 服务和平面文件输出。
ps: I've recently moved the code to github
ps:我最近把代码移到了github

