Manipulating fixed formatted text files
Posted by Jacob von Eyben on March 1st, 2008
I am working on a project where we need to manipulate fixed formatted text files. The fixed formatted files are used as protocol for interchanging data between some legacy mainframe systems. I have been looking for some already existing api for manipulating these fixed formatted data but couldn’t find any.
So this weekend I started implementing a solution based on some of the idea´s we got along the way.
Basics
I have a few goals for the api:
- A line of text should be easily manipulated as a java object.
- Should require a minimum of own code to manipulate the text string.
- Use annotations to define attributes like:
- Offset in text
- The fixed length of the text
- padding direction and padding character
The api usage could look something like this:
public interface Record {
// use if you need a reference to the manipulated string - could be a constructor
void initialize(StringBuffer buffer);
// use if you don't need a reference to the manipulated string - could be a constructor
void initialize(String string);
String export(); //export string
}
...
public abstract class MyRecord implements Record {
@FixedFormatField(offset = 10, length=20, paddingChar='0')
public abstract void setMyInteger(Integer myInteger);
@FixedFormatField(offset = 10, length=20, paddingChar='0')
public abstract Integer getMyInteger();
}
And then a factory to create instances of the Record. Maybe annotations on fields would be nice as well as dublicating annotations for setter/getter is waste and could be error-prone.
This is the part I am working on at the moment and getting some hands-on experience with javassist. I will get back to that in another post.
Fixedformat4j
I have named the api fixedformat4j and I will opensource it as soon as I have something worth for others to use. Hopefully it won’t end up as one of my numerous 23% finished projects…
March 3rd, 2008 at 1:59 pm
Hey,
You might want to take a look at JFileHelpers library I ported from C#’s FileHelpers. Take a look at this - translated from Portuguese - article:
http://tinyurl.com/38rawn
Let me know what your thoughts are.
Best regards,
Felipe Coury
March 3rd, 2008 at 2:00 pm
… the code snippets were pretty much messed up upon automatic translation, so for referencing to snippets, take a look at the original article:
http://blog.felipecoury.com/jep/2008/02/java-text-import-export.html
Regards,
Felipe.
March 3rd, 2008 at 2:27 pm
Jacob,
I wrote some code a while ago. You might see a interface I made in order to let you easily build a set of beans able to handle a given record @ http://www.leal.eng.br/apps/mapper/launch
March 3rd, 2008 at 5:42 pm
if you can talk to the mainframe developers, you can get the cobol data structures and then use that to quickly automate writing the java classes. The perl world did this a looog time ago.
See this http://search.cpan.org/~grommel/Convert-IBM390-0.23/IBM390.pod
March 5th, 2008 at 4:34 pm
Take a peek, maybe it fits your needs:
jffp
March 17th, 2008 at 9:13 pm
[...] Manipulating fixed formatted text files [...]
June 7th, 2008 at 9:59 pm
I have just created the website for JFileHelpers, so if you are interested on joining forces:
http://www.jfilehelpers.com