Examples >

Write a CSV File to Fixed Width

This example shows you how to write the contents of a CSV file to a Fixed Width file in Java using the CSVReader and FixedWidthWriter classes. A Fixed Width text file is a file that has a specific format which allows for the saving of textual information/data in an organised fashion.

Other input sources like Excel, Database, etc may also be used to write to a fixed-width file instead of CSV.

This example can easily be modified to show how to read a Fixed Width file.

Input CSV file

868,Hewitt,Jennifer Love,0,17000.00,5/25/1985,B

Java Code listing

 * 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 com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.fixedwidth.FixedWidthWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.job.JobTemplate;

public class WriteACsvFileToFixedWidth {
    public static void main(String[] args) throws Throwable {
        DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
        FixedWidthWriter writer = new  FixedWidthWriter(new File("example/data/output/credit-balance-02.fw"));
        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.


CSVReader is an input reader which can be used to read CSV files. It is a sub-class of TextReader and inherits the open and close among other methods. The CSVReader.setFieldNamesInFirstRow(true) method causes the CSVReader to use the names specified in the first row of the input data as field names. If this method is not invoked, the fields would be named as A1, A2, etc. similar to MS Excel. If those fields names need to be changed, a rename transformation can be added on top of CSVReader or any other type (Refer Rename a field for example).


The FixedWidthWriter class can be used to create Fixed Width files. Its addField(s) needs to be invoked for the fields that need to be written to the output file. This method accepts an integer as the parameter which specifies the width of the corresponding field in the output file. The value specified here should be large enough to accommodate the largest field value, otherwise the field value will get truncated. The default fill character is the space character, but this can be changed for the entire reader or for individual fields.

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