Skip First Line Using Limit Reader

Updated: Jan 3, 2023
CSV

In this example you will learn how to skip the first line while reading data using LimitReader.

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 listing

package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.LimitReader;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;

import java.io.File;

public class SkipFirstLineUsingLimitReader {

    private static final int MAX_RESULTS = 3;
    private static final int SKIPPED_LINES = 1;
    
    public static void main(String[] args) throws Throwable {
        DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
                .setFieldNamesInFirstRow(true);

        reader = new LimitReader(reader, SKIPPED_LINES, MAX_RESULTS);

        Job.run(reader, new StreamWriter(System.out));
    }
    
}

Code walkthrough

  1. MAX_RESULTS represents the number of records to be read, i.e. the number of lines in the file to be read.
  2. SKIPPED_LINES is the number of lines to be skipped.
  3. A CSVReader is created using the file path of the input file credit-balance-01.csv.
  4. The CSVReader.setFieldNamesInFirstRow(true) method is invoked to specify that the names specified in the first row should be used as field names.
  5. LimitReader is then used to skip the first record which is the second line in the CSV file since the first line will be used as field names.
  6. LimitReader is also used to limit the number of records sent downstream, i.e for this caseit limit the records to 3.
  7. Job.run() method which transfers data from the reader to the StreamWriter() is then called.
  8. StreamWriter finally output the records to the output console.

Console Output

-----------------------------------------------
0 - Record {
    0:[Account]:STRING=[312]:String
    1:[LastName]:STRING=[Butler]:String
    2:[FirstName]:STRING=[Gerard]:String
    3:[Balance]:STRING=[90.00]:String
    4:[CreditLimit]:STRING=[1000.00]:String
    5:[AccountCreated]:STRING=[8/6/2003]:String
    6:[Rating]:STRING=[B]:String
}

-----------------------------------------------
1 - Record {
    0:[Account]:STRING=[868]:String
    1:[LastName]:STRING=[Hewitt]:String
    2:[FirstName]:STRING=[Jennifer Love]:String
    3:[Balance]:STRING=[0]:String
    4:[CreditLimit]:STRING=[17000.00]:String
    5:[AccountCreated]:STRING=[5/25/1985]:String
    6:[Rating]:STRING=[B]:String
}

-----------------------------------------------
2 - Record {
    0:[Account]:STRING=[761]:String
    1:[LastName]:STRING=[Pinkett-Smith]:String
    2:[FirstName]:STRING=[Jada]:String
    3:[Balance]:STRING=[49654.87]:String
    4:[CreditLimit]:STRING=[100000.00]:String
    5:[AccountCreated]:STRING=[12/5/2006]:String
    6:[Rating]:STRING=[A]:String
}

-----------------------------------------------
Mobile Analytics