Skip to content

Instantly share code, notes, and snippets.

@timothyshort
Last active March 30, 2022 08:19
Show Gist options
  • Select an option

  • Save timothyshort/e17dda748b4b3ab83cb135589037f786 to your computer and use it in GitHub Desktop.

Select an option

Save timothyshort/e17dda748b4b3ab83cb135589037f786 to your computer and use it in GitHub Desktop.
Read a CSV file and return a List of arrays
// This method will read a CSV file and return a List of String[]
public static List<String[]> get(String filename) {
List<String[]> data = new ArrayList<String[]>();
String testRow;
try {
// Open and read the file
BufferedReader br = new BufferedReader(new FileReader(filename));
// Read data as long as it's not empty
// Parse the data by comma using .split() method
// Place into a temporary array, then add to List
while ((testRow = br.readLine()) != null) {
String[] line = testRow.split(",");
data.add(line);
}
} catch (FileNotFoundException e) {
System.out.println("ERROR: File not found " + filename);
} catch (IOException e) {
System.out.println("ERROR: Could not read " + filename);
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment