Converting Flat File to Java Objects [closed]
Asked Answered
K

6

7

Does anybody know of any good libraries to convert a flat file to Java objects? I found flatworm but I am looking for alternatives.

Keaton answered 18/8, 2009 at 14:53 Comment(5)
If it's something like a CSV (or any delimited file) or XML, it's trivial to read it in and create objects out of it. Could you elaborate on the file format and what you are trying to do?Part
Flat file is a pretty broad category. Text file? CSV file? Serialized Object file? What's in the "flat file"Engineman
nothing wrong with it, just want to see if there are alternatives that might be better suited to my needsKeaton
flat files are fixed width column text files - correct. i need to parse such files.Keaton
You are looking for FFPOJO: github.com/ffpojo/ffpojoUnwholesome
E
4

FFP - Flat file parsing library
http://jffp.sourceforge.net/

Emend answered 18/8, 2009 at 15:13 Comment(0)
F
9

Quick update: flatworm has not been active for quite a while, there is a fork named BeanIO: http://www.beanio.org/

Frink answered 5/3, 2012 at 13:1 Comment(0)
E
4

FFP - Flat file parsing library
http://jffp.sourceforge.net/

Emend answered 18/8, 2009 at 15:13 Comment(0)
M
2

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

Meter answered 27/2, 2013 at 7:25 Comment(0)
A
2

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");
Arachne answered 19/5, 2014 at 16:10 Comment(0)
L
1

You can also give a try to Fixedformat4j. I like the annotations approach and it's very simple to define a custom field format.

Lunula answered 2/3, 2010 at 8:11 Comment(0)
A
1

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

Adoree answered 28/11, 2012 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.