I have a flat file in which data is stored in position based format For eg. from 1 to 5 - some x value is stored, from 6 to 13 - some y value is stored, from 14 to 18 - some z value is stored and so on.. I need to parse the file and get those values and populate a bean.
Can anyone please tell me the best way I can go about it means how I can parse the file.I am using Java 6.
Non complex, fixed length rows should be very easy in plain java.
Why don't you just use plain basic substring? I've seen this used in parsing quite big flat files, and it's not as bad as it sounds. Pretty easy to get an overview from it as well.
myObject.setX(Integer.parseInt(input.substring(0,4)));
myObject.setY(input.substring(5,12);
..
If you are really serious in mapping several large flat files to java, you might want to use some library.
Smooks let's you specify the mapping in a XML file, and have the smooks runtime map from fields to an object. There is also an Eclipse IDE for graphical mapping. This library is somewhat heavyweight.
I really like the Bindy component in Apache Camel. It requires the overhead of introducing a message router, but it's possible to annotate plain java classes and just do the mapping and the java class in one go.
// Like this
@FixedLengthRecord(length=54, paddingChar=' ')
public static class Order {
@DataField(pos = 1, length=2)
private int orderNr;
@DataField(pos = 3, length=2)
private String clientNr;
来源:https://stackoverflow.com/questions/14324450/parsing-a-flat-file-in-java