Reading csv file into array

I am completely new to java (this week only). I am trying to read the csv file, read_ex.csv, into an array. I searched endlessly on the Internet / satckoverflow to find a way to read a file in an array. The best I could do was read it in a streaming way, but I cannot save it in an array due to the variable file size. I believe ArrayList is a method of working with an array of variable sizes, but I do not know how to work with it. Essentially, I want to have access to the array of "values" of the array of strings after the while loop completes.

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); try{ Scanner inputStream= new Scanner(file); while(inputStream.hasNext()){ String data= inputStream.next(); String[] values = data.split(","); System.out.println(values[1]); } inputStream.close(); }catch (FileNotFoundException e) { e.printStackTrace(); } //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 to open any method that reads csv to a file that a newbie can understand.

+6
source share
2 answers

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

 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; try{ inputStream = new Scanner(file); while(inputStream.hasNext()){ String line= inputStream.next(); String[] values = line.split(","); // this adds the currently parsed line to the 2-dimensional string array lines.add(Arrays.asList(values)); } inputStream.close(); }catch (FileNotFoundException e) { e.printStackTrace(); } // 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); columnNo++; } lineNo++; } } } 

Skip it step by step:

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

  • Each class name in Java starts with a capital letter (by convention - it will be built with a small letter, but you should get used to this convention) - I "fixed" this in your code.

  • I added a 2-dimensional array List<List<String>> lines = new ArrayList<>() . This may seem a bit confusing at first, but that means that we are creating the lines variable that contains the results of our parsing. The syntax of List<String> means that we have a generic List type that has a parameter of type String - in other words: a list of strings. The whole List<List<String>> means that we have a list of string lists, a 2-dimensional string array.

  • Using lines.add(Arrays.asList(values)) in your while we can add the lines you analyzed in this two-dimensional array. Arrays.asList(values) converts the String[] array to List , because we need it to be compatible with our type List<List<...>> . This allows your lines to have a variable length.

  • The last lines that I added just print the contents of the two-dimensional array and should give you a good example of how to access the values ​​in this array. If you need more help for this design, check out the foreach loop documentation .

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

 value_1-1,value_1-2,value_1-3,value_1-4 value_2-1,value_2-2,value_2-3,value_2-4 value_3-1,value_3-2,value_3-3,value_3-4 value_4-1,value_4-2,value_4-3,value_4-4 value_5-1,value_5-2,value_5-3,value_5-4 

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

+12
source

logo.png

I am new to java and wold to open any method that reads csv to a file that a newbie can understand.

Here is an existing solution for you from Apache Commons , Apache Commons CSV Project . See the Apache Commons CSV Parser Guide .

It is very simple to use out of the box.

Here is the basic worfklow:

  • Create a class that has fields corresponding to csv columns.
  • Each line of your csv is an object of this class with properties corresponding to the fields of your csv. Use the library to get each field and store it in the object in a loop.
  • In your loop you will save an object with fields / properties read from csv to ArrayList
  • After the loop finishes, your ArrayList will have elements that "match" the lines in your csv file. You can access any of the lines / items in the list.

If you are not familiar with how List and ArrayList works in Java, please check out all the Java tutorials , including the Oracle Tutorial .

Search Stackoverflow for hundreds of Commons CSV usage reports.

+6
source

All Articles