Read a CSV File

Updated: Feb 21, 2022
CSV

This example shows you how to read a CSV file in Java using the CSVReader class.

This example be be easily modified to show how to write to a CSV file.

Input CSV file

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

Java code listing

  1. package com.northconcepts.datapipeline.examples.cookbook;
  2.  
  3. import java.io.File;
  4.  
  5. import org.apache.log4j.Logger;
  6.  
  7. import com.northconcepts.datapipeline.core.DataEndpoint;
  8. import com.northconcepts.datapipeline.core.DataReader;
  9. import com.northconcepts.datapipeline.core.Record;
  10. import com.northconcepts.datapipeline.csv.CSVReader;
  11.  
  12. public class ReadACsvFile {
  13. public static final Logger log = DataEndpoint.log;
  14.  
  15. public static void main(String[] args) {
  16. DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
  17. .setFieldNamesInFirstRow(true);
  18.  
  19. reader.open();
  20. try {
  21. Record record;
  22. while ((record = reader.read()) != null) {
  23. log.info(record);
  24. }
  25. } finally {
  26. reader.close();
  27. }
  28. }
  29.  
  30. }
  31.  

Code walkthrough

  1. A CSVReader is created using the file path of the input file credit-balance-01.csv.
  2. The CSVReader.setFieldNamesInFirstRow(true) method is invoked to specify that the names specified in the first row should be used as field names.
  3. The CSVReader is opened via reader.open method.
  4. A while loop iterates through the input data.
  5. Each row in the input CSV is read as a Record object and printed to the console via the Datapipeline logger.
  6. The CSVReader is closed via reader.close method in a finally block after the while loop completes.

CSVReader

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).

Console output

  1. 15:43:35,278 INFO [main] datapipeline:31 - Record {
  2. 0:[Account]:STRING=[101]:String
  3. 1:[LastName]:STRING=[Reeves]:String
  4. 2:[FirstName]:STRING=[Keanu]:String
  5. 3:[Balance]:STRING=[9315.45]:String
  6. 4:[CreditLimit]:STRING=[10000.00]:String
  7. 5:[AccountCreated]:STRING=[1/17/1998]:String
  8. 6:[Rating]:STRING=[A]:String
  9. }
  10.  
  11. 15:43:35,278 INFO [main] datapipeline:31 - Record {
  12. 0:[Account]:STRING=[312]:String
  13. 1:[LastName]:STRING=[Butler]:String
  14. 2:[FirstName]:STRING=[Gerard]:String
  15. 3:[Balance]:STRING=[90.00]:String
  16. 4:[CreditLimit]:STRING=[1000.00]:String
  17. 5:[AccountCreated]:STRING=[8/6/2003]:String
  18. 6:[Rating]:STRING=[B]:String
  19. }
  20.  
  21. 15:43:35,279 INFO [main] datapipeline:31 - Record {
  22. 0:[Account]:STRING=[868]:String
  23. 1:[LastName]:STRING=[Hewitt]:String
  24. 2:[FirstName]:STRING=[Jennifer Love]:String
  25. 3:[Balance]:STRING=[0]:String
  26. 4:[CreditLimit]:STRING=[17000.00]:String
  27. 5:[AccountCreated]:STRING=[5/25/1985]:String
  28. 6:[Rating]:STRING=[B]:String
  29. }
  30.  
  31. 15:43:35,279 INFO [main] datapipeline:31 - Record {
  32. 0:[Account]:STRING=[761]:String
  33. 1:[LastName]:STRING=[Pinkett-Smith]:String
  34. 2:[FirstName]:STRING=[Jada]:String
  35. 3:[Balance]:STRING=[49654.87]:String
  36. 4:[CreditLimit]:STRING=[100000.00]:String
  37. 5:[AccountCreated]:STRING=[12/5/2006]:String
  38. 6:[Rating]:STRING=[A]:String
  39. }
  40.  
  41. 15:43:35,280 INFO [main] datapipeline:31 - Record {
  42. 0:[Account]:STRING=[317]:String
  43. 1:[LastName]:STRING=[Murray]:String
  44. 2:[FirstName]:STRING=[Bill]:String
  45. 3:[Balance]:STRING=[789.65]:String
  46. 4:[CreditLimit]:STRING=[5000.00]:String
  47. 5:[AccountCreated]:STRING=[2/5/2007]:String
  48. 6:[Rating]:STRING=[C]:String
  49. }
Mobile Analytics