Last active
March 30, 2022 08:19
-
-
Save timothyshort/e17dda748b4b3ab83cb135589037f786 to your computer and use it in GitHub Desktop.
Read a CSV file and return a List of arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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