mikeL mikeL - 4 days ago 4x
Java Question

reading a csv file into a array

I am completely new to java (just started this week). I am trying to read the csv file, "read_ex.csv", into an array. I have searched endlessly on the web/satckoverflow to find a way to read the file into an array. The best i have been able to do is read it in a streaming fashion, but I cant store it in an array due the variable size of the file. I belive that ArrayList is a method to deal with variable size array, but I don't know how to work with it. Essentially I want to be able to access the String array "values" after the while loop finishes.

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;

public class sortarray {
public static void main (String []agrs){
String fileName= "read_ex.csv";
File file= new File(fileName);

Scanner inputStream= new Scanner(file);
String data= inputStream.next();
String[] values = data.split(",");
}catch (FileNotFoundException e) {
//This prints out the working directory
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));



As i mentioned, I am new to java and wold be open to any method that reads a csv into a file that a beginner could understand.


Although using the Apache CSV library as mentioned by @Minjun.Y is perfectly fine, I try to provide a solution that is closer to your code and maybe easier for you to follow:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class CsvParser {

    public static void main(String[] args) {
        String fileName= "read_ex.csv";
        File file= new File(fileName);

        // this gives you a 2-dimensional array of strings
        List<List<String>> lines = new ArrayList<>();
        Scanner inputStream;

            inputStream = new Scanner(file);

                String line= inputStream.next();
                String[] values = line.split(",");
                // this adds the currently parsed line to the 2-dimensional string array

        }catch (FileNotFoundException e) {

        // the following code lets you iterate through the 2-dimensional array
        int lineNo = 1;
        for(List<String> line: lines) {
            int columnNo = 1;
            for (String value: line) {
                System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);


Let's go through it step-by-step:

  1. I added 3 more imports: ArrayList, Arrays and List - you will see very soon for what they are good. They are all taken from the java.util library, a standard library that is available with every JDK.

  2. Every class name in Java starts with a capital letter (by convention - it will build with a small letter as well, but you should get used to this convention) - I "fixed" this in your code.

  3. I added a 2-dimensional array List<List<String>> lines = new ArrayList<>(). This might look a little confusing at first, but what it means is that we create a variable lines that holds the results of our parsing. The List<String> syntax means, that we have a generic type List that has a type parameter String - in other words: a list of strings. The whole List<List<String>> means we have a list of lists of strings, a 2-dimensional string array.

  4. With the lines.add(Arrays.asList(values)) in your while loop, we can add the lines that you parsed to this 2-dimensional array. Arrays.asList(values) transforms the String[] array into a List as we need it to be compatible to our List<List<...>> type. This allows your lines to have variable length.

  5. The last lines I added simply print the content of 2-dimensional array and should give you a good example for how to access the values in this array. If you need further help for this construct, check the foreach loop documentation.

Given this as an input file (read_ex.csv):


The program will print the following output:

Line 1 Column 1: value_1-1
Line 1 Column 2: value_1-2
Line 1 Column 3: value_1-3
Line 1 Column 4: value_1-4
Line 2 Column 1: value_2-1
Line 2 Column 2: value_2-2
Line 2 Column 3: value_2-3
Line 2 Column 4: value_2-4
Line 3 Column 1: value_3-1
Line 3 Column 2: value_3-2
Line 3 Column 3: value_3-3
Line 3 Column 4: value_3-4
Line 4 Column 1: value_4-1
Line 4 Column 2: value_4-2
Line 4 Column 3: value_4-3
Line 4 Column 4: value_4-4
Line 5 Column 1: value_5-1
Line 5 Column 2: value_5-2
Line 5 Column 3: value_5-3
Line 5 Column 4: value_5-4

Hope this helps :)