问题
Does anybody know of any good libraries to convert a flat file to Java objects? I found flatworm but I am looking for alternatives.
回答1:
FFP - Flat file parsing library
http://jffp.sourceforge.net/
回答2:
Quick update: flatworm has not been active for quite a while, there is a fork named BeanIO: http://www.beanio.org/
回答3:
Another alternative, that I wrote that uses Java Annotations is JFileHelpers - http://jfilehelpers.com
An example of annotated 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");
回答4:
I have not used this JFlat, But it seems this Framework provides conversion from Flat file to Java object.
Similarly BeanIO and Jsefa also 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
Apache Camel has Flatpack component as well as from 2.10 it has BeanIO component
回答5:
You would like to consider JRecordBind (I'm its author)
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.
ps: I've recently moved the code to github
回答6:
You can also give a try to Fixedformat4j. I like the annotations approach and it's very simple to define a custom field format.
来源:https://stackoverflow.com/questions/1294405/converting-flat-file-to-java-objects