How to Read a CSV File Into Java

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Read a CSV File Into Java
(Ciaran Griffin/Lifesize/Getty Images)

CSV, or Comma Separated Value, files are commonly used as a universal format for exchanging simple data, especially between spreadsheet applications. The format is simple. Take a table of data and place the name of each column on the first line, with each name separated from the others by a comma. Then, go through the full table, placing each row on a line with each column separated by commas. Java does not have a built in utility for reading CSV files, but that shouldn't stop you from using them in your Java application. The simplistic format makes it easy to create your own Java CSV file parser.

Skill level:


  1. 1

    Create a new class named by pasting the following into an empty text file:

    import; import; import java.util.ArrayList; import java.util.Scanner; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel;

    public class CSVParser { }

  2. 2

    Paste the following "parse" method into it between the brackets in the class:

    public static TableModel parse(File f) throws FileNotFoundException {
        ArrayList<String> headers = new ArrayList<String>();
        ArrayList<String> oneDdata = new ArrayList<String>();

    // Get the headers of the table. Scanner line Scan = new Scanner(f); Scanner s = new Scanner(lineScan.nextLine()); s.useDelimiter(","); while (s.hasNext()) { headers.add(; }

    // Go through each line of the table and add each cell to the ArrayList while (lineScan.hasNextLine()) { s = new Scanner(lineScan.nextLine()); s.useDelimiter(", *"); while (s.hasNext()) { oneDdata.add(; } } String[][] data = new String[headers.size()][oneDdata.size()/headers.size()]; // Move the data into a vanilla array so it can be put in a table. for (int x = 0; x < headers.size(); x++) { for (int y = 0; y < data[0].length; y++) { data[x][y] = oneDdata.remove(0); } } // Create a table and return it. return new DefaultTableModel(data, headers.toArray());

  3. 3

    Paste the following main method just beneath the parse method from Step 2:

    public static void main(String[] args) throws FileNotFoundException {
    &#9;// Call the parse method and put the results in a table.
        TableModel t = CSVParser.parse(new File("test.csv"));

    // Print all the columns of the table, followed by a new line. for (int x = 0; x < t.getColumnCount(); x++) { System.out.print(t.getColumnName(x) + " "); } System.out.println();

    // Print all the data from the table. for (int x = 0; x < t.getRowCount(); x++) { for (int y = 0; y < t.getColumnCount(); y++) { System.out.print(t.getValueAt(x, y) + " "); } System.out.println(); }


Don't Miss


  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.