Howto deal with fixed fields text record without substring

Howto avoid substring approach to deal with fixed fields formatted text records. The solution is the Java library Fixefid

Often we have to develop Java methods to deal with fixed fields formatted text records. The format is often used nowadays yet to files transfer between mainframe and unix machines. Almost always the approach is using substring like this:

String address = record.substring(10, 20);
String location = record.substring(20, 30);

But that is an error prone pattern. The records are length thousands of chars with many many fields and make a mistake with the offset is not so rarely. And moreover when you have to format the fields to create the record, you have to deal with string right padding, zero lead numeric left padding, boolean format, date format, decimal format, decimal separator, etc etc…

The solution is using a Java library like Fixefid, wich permits to start from a specification in excel, model the record with Java Bean or Java Enum and deal with fields by getter, setter and toString Java methods.

I’ve made a video tutorial where I explain in the detail way the process to start from an excel like this:

to create a Java Bean model like this:

to obtain a records txt file like this:

In this way we can using the standard java bean development to deal with fixed fields formatted text files.

For example you can create a Java Bean like this:

@FixefidRecord(recordLen = 600)
public class Student extends Person {
@FixefidField(fieldLen = 10, fieldOrdinal = 8, fieldType = FieldType.N,       
         fieldMandatory = FieldMandatory.INOUT)
private Long studentId;
….
}

and create a BeanRecord like this:

Student student = createStudent();
BeanRecord studentRecord = new BeanRecord(student, null, null, 
       createStudentMapFieldExtendedProperties());
studentRecord.toNormalize();
String studentRecordAsString = studentRecord.toString();
System.out.println("Student Record=[" + studentRecordAsString + "]");

The video tutorial explains the whole process in the detail way.

Here the java project developed on the video tutorial.

If you don’t want to use Java Bean, the same record can be modeled with Java Enum.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s