4

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.

Anand
  • 20,708
  • 48
  • 131
  • 198
  • 3
    At what step exactly are you stucking? Reading the file line by line using e.g. `BufferedReader#readLine()` or substringing the line using e.g. `String#substring()`? It are fairly straightforward methods though. They are already mentioned in Oracle's basic Java tutorial. – BalusC Jan 14 '13 at 18:39
  • 2
    You should try something yourself first and then come back with a more specific question if you run into an issue. – James Montagne Jan 14 '13 at 18:39
  • You could use a [`java.util.Scanner`](http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html) to read every line of the flat file. If there is a pattern that separates the data like a pipe (|) or a comma (,), you can use [`String#split`](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29) to separate the data and distribute it, if this is not your case you can use [`String#substring`](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#substring%28int,%20int%29) to split the data partially. – Luiggi Mendoza Jan 14 '13 at 18:42

1 Answers1

4

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;
Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84