diff --git a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java index 29d1ec0a47175298a9eb7ee8ee805b843e2b27fb..6541434f3f688cea6e7cc81870565d7818f6f2b6 100644 --- a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java +++ b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java @@ -12,6 +12,7 @@ import java.io.*; import java.net.URL; import java.util.*; +@SuppressWarnings("unused") public class CSVReaderWriter { @SuppressWarnings("WeakerAccess") public static Set<Map<String, String>> readCSVasSet(String filename) { @@ -32,6 +33,24 @@ public class CSVReaderWriter { return csvSet; } + public static List<Map<String, String>> readCSVasList(String filename) { + List<Map<String, String>> csvList = null; + try (InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull( + CSVReaderWriter.class.getClassLoader().getResourceAsStream("csv/" + filename)))) { + csvList = (List<Map<String, String>>) parseCSV(inputStreamReader, new LinkedList<>()); + } catch (NullPointerException nullPointerException) { + System.err.println("Check spelling of file " + filename + "."); + System.exit(1); + } catch (IOException ioException) { + System.err.println("Error loading " + filename + ". " + ioException); + System.exit(1); + } catch (CsvValidationException csvValidationException) { + System.err.println("Could not validate a line in " + filename + ". " + csvValidationException); + System.exit(1); + } + return csvList; + } + private static Collection<Map<String, String>> parseCSV(InputStreamReader inputStreamReader, Collection<Map<String, String>> destination) throws IOException, CsvValidationException { @@ -48,7 +67,6 @@ public class CSVReaderWriter { } /* LEGACY METHODS */ - @SuppressWarnings("WeakerAccess") public static Set<Map<String, String>> readCSV(String filename) { return readCSVasSet(filename); } @@ -65,6 +83,8 @@ public class CSVReaderWriter { } */ + // I'd like to replace this with something that uses openCSV, but it works, and openCSV doesn't seem to have + // writers that take map<string,string> public static boolean writeCSV(String filename, Set<Map<String, String>> data) { boolean wroteFile = true; ClassLoader classLoader = CSVReaderWriter.class.getClassLoader(); @@ -147,42 +167,11 @@ public class CSVReaderWriter { } } } - - /* - public static void main(String[] args) { - Set<Map<String, String>> demo = readCSV("demo.csv"); - boolean success = writeCSV("out.csv", demo); - System.out.println(success ? "Wrote file!" : "Didn't write file"); - } - */ /* - public static void main(String[] args) { - Set<Map<String,String>> students = readCSV("students.csv"); - for (Map<String,String> student: students) { - System.out.println(student.get("Name")); - } - } -*//* - public static void main(String[] args) { - Set<Map<String, String>> courses = readCSV("courses.csv"); - for (Map<String, String> course : courses) { - String foo = course.get("URL"); - if (foo == null) { - foo = "Null object"; - } else if (foo.equals("")) foo = "Empty String"; - System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get( - "Prerequisite1")); - } - }*/ - public static void main(String[] args) { - Set<Map<String, String>> courses = readCSV("empty.csv"); - for (Map<String, String> course : courses) { - String foo = course.get("URL"); - if (foo == null) { - foo = "Null object"; - } else if (foo.equals("")) foo = "Empty String"; - System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get( - "Prerequisite1")); - } + public static void main(String[] args) { + Set<Map<String, String>> demo = readCSV("demo.csv"); + boolean success = writeCSV("out.csv", demo); + System.out.println(success ? "Wrote file!" : "Didn't write file"); } + */ } diff --git a/src/main/resources/csv/demo.csv b/src/main/resources/csv/demo.csv index 8e1318e6a4a16734c6827d08cb6a50f230652e99..c0e808529d06101dc0e15eaa9c56c14c07593309 100644 --- a/src/main/resources/csv/demo.csv +++ b/src/main/resources/csv/demo.csv @@ -1,4 +1,5 @@ A,B,C,D +larry,curly,moe, one,two,,three four,,five,six 7,8,9,