Examples >

Read a Fixed-width File / Fixed-length Record File

The FixedWidthReader can be used to parse fixed-width / fixed-length record (FLR) text files and input streams.

The following fixed-width file (credit-balance-01.fw) will be used as the input for this example.

Account LastName        FirstName       Balance     CreditLimit   AccountCreated  Rating 
101     Reeves          Keanu           9315.45     10000.00      1/17/1998       A      
312     Butler          Gerard          90.00       1000.00       8/6/2003        B      
868     Hewitt          Jennifer Love   0           17000.00      5/25/1985       B      
761     Pinkett-Smith   Jada            49654.87    100000.00     12/5/2006       A      
317     Murray          Bill            789.65      5000.00       2/5/2007        C      

FixedWidthReader requires that you either call addField(s) for the fields you wish to read in or skipField with the number of characters to skip before the next field.

This example uses space as the fill char (the default), but this can be changed for the entire reader or for individual fields.

/*
 * Copyright (c) 2006-2018 North Concepts Inc.  All rights reserved.
 * Proprietary and Confidential.  Use is subject to license terms.
 *
 * http://northconcepts.com/data-pipeline/licensing/
 *
 */
package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import org.apache.log4j.Logger;

import com.northconcepts.datapipeline.core.DataEndpoint;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.fixedwidth.FixedWidthReader;

public class ReadAFixedWidthFile {
    
    public static final Logger log = DataEndpoint.log; 

    public static void main(String[] args) {
        FixedWidthReader reader = new FixedWidthReader(new File("example/data/input/credit-balance-01.fw"));

        reader.setFieldNamesInFirstRow(true);
        
        reader.addFields(8);
        reader.addFields(16);
        reader.addFields(16);
        reader.skipField(12);  // ignore field 3 - Balance
        reader.addFields(14);
        reader.addFields(16);
        reader.addFields(7);

        reader.open();
        try {
            Record record;
            while ((record = reader.read()) != null) {
                log.info(record);
            }
        } finally {
            reader.close();
        }
    }
}

The resulting output would look like the following.

22:53:25,229  INFO [main] datapipeline:39 - Record {
    0:[Account]:STRING=[101]:String
    1:[LastName]:STRING=[Reeves]:String
    2:[FirstName]:STRING=[Keanu]:String
    3:[CreditLimit]:STRING=[10000.00]:String
    4:[AccountCreated]:STRING=[1/17/1998]:String
    5:[Rating]:STRING=[A]:String
}

22:53:25,238  INFO [main] datapipeline:39 - Record {
    0:[Account]:STRING=[312]:String
    1:[LastName]:STRING=[Butler]:String
    2:[FirstName]:STRING=[Gerard]:String
    3:[CreditLimit]:STRING=[1000.00]:String
    4:[AccountCreated]:STRING=[8/6/2003]:String
    5:[Rating]:STRING=[B]:String
}

22:53:25,240  INFO [main] datapipeline:39 - Record {
    0:[Account]:STRING=[868]:String
    1:[LastName]:STRING=[Hewitt]:String
    2:[FirstName]:STRING=[Jennifer Love]:String
    3:[CreditLimit]:STRING=[17000.00]:String
    4:[AccountCreated]:STRING=[5/25/1985]:String
    5:[Rating]:STRING=[B]:String
}

22:53:25,241  INFO [main] datapipeline:39 - Record {
    0:[Account]:STRING=[761]:String
    1:[LastName]:STRING=[Pinkett-Smith]:String
    2:[FirstName]:STRING=[Jada]:String
    3:[CreditLimit]:STRING=[100000.00]:String
    4:[AccountCreated]:STRING=[12/5/2006]:String
    5:[Rating]:STRING=[A]:String
}

22:53:25,242  INFO [main] datapipeline:39 - Record {
    0:[Account]:STRING=[317]:String
    1:[LastName]:STRING=[Murray]:String
    2:[FirstName]:STRING=[Bill]:String
    3:[CreditLimit]:STRING=[5000.00]:String
    4:[AccountCreated]:STRING=[2/5/2007]:String
    5:[Rating]:STRING=[C]:String
}
Mobile Analytics