Write a CSV File to a Fixed Width / Fixed Length Record File

Updated: Jun 24, 2023

This example is designed to convert a CSV file into a fixed-width format, where each field is aligned to a specific position or width in the output file. It provides functionality to define the field widths and alignments, allowing users to customize the format as per their requirements.

Real-life use cases for this library can be found in scenarios where data needs to be formatted in a specific way for downstream systems or legacy applications that expect fixed-width file extensions. For example, when generating data files for financial systems or mainframe applications, which often require fixed-width formats, this example becomes valuable. It simplifies the process of converting CSV files into the required format, ensuring that the data is correctly aligned and meets the specifications of the target system.

Input CSV file

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

 

Java Code

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.fixedwidth.FixedWidthWriter;
import com.northconcepts.datapipeline.job.Job;

public class WriteACsvFileToFixedWidth {
    
    public static void main(String[] args) throws Throwable {
        DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
            .setFieldNamesInFirstRow(true);
        
        FixedWidthWriter writer = new  FixedWidthWriter(new File("example/data/output/credit-balance-02.fw"));
        writer.addFields(8);
        writer.addFields(16);
        writer.addFields(16);
        writer.addFields(12);
        writer.addFields(14);
        writer.addFields(16);
        writer.addFields(7);
        
        Job.run(reader, writer);
    }

}

 

Code Walkthrough

  1. A CSVReader is created corresponding to the input CSV file credit-balance-01.csv.
  2. A FixedWidthWriter is created corresponding to the output Fixed Width file i.e. credit-balance-02.fw.
  3. The writer.addField(s) method is invoked for the fields that need to be written to the output file.
  4. Data is transferred from the input CSV file to the output Fixed Width file via the JobTemplate.DEFAULT.transfer method.

 

Output Fixed Width file

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      
Mobile Analytics