Skip to content
Snippets Groups Projects
Commit 4821aa8f authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Added reader that returns List, for preserving row order

parent 63ffe1e7
Branches
Tags v1.8
No related merge requests found
...@@ -12,6 +12,7 @@ import java.io.*; ...@@ -12,6 +12,7 @@ import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
@SuppressWarnings("unused")
public class CSVReaderWriter { public class CSVReaderWriter {
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public static Set<Map<String, String>> readCSVasSet(String filename) { public static Set<Map<String, String>> readCSVasSet(String filename) {
...@@ -32,6 +33,24 @@ public class CSVReaderWriter { ...@@ -32,6 +33,24 @@ public class CSVReaderWriter {
return csvSet; 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, private static Collection<Map<String, String>> parseCSV(InputStreamReader inputStreamReader,
Collection<Map<String, String>> destination) Collection<Map<String, String>> destination)
throws IOException, CsvValidationException { throws IOException, CsvValidationException {
...@@ -48,7 +67,6 @@ public class CSVReaderWriter { ...@@ -48,7 +67,6 @@ public class CSVReaderWriter {
} }
/* LEGACY METHODS */ /* LEGACY METHODS */
@SuppressWarnings("WeakerAccess")
public static Set<Map<String, String>> readCSV(String filename) { public static Set<Map<String, String>> readCSV(String filename) {
return readCSVasSet(filename); return readCSVasSet(filename);
} }
...@@ -65,6 +83,8 @@ public class CSVReaderWriter { ...@@ -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) { public static boolean writeCSV(String filename, Set<Map<String, String>> data) {
boolean wroteFile = true; boolean wroteFile = true;
ClassLoader classLoader = CSVReaderWriter.class.getClassLoader(); ClassLoader classLoader = CSVReaderWriter.class.getClassLoader();
...@@ -147,7 +167,6 @@ public class CSVReaderWriter { ...@@ -147,7 +167,6 @@ public class CSVReaderWriter {
} }
} }
} }
/* /*
public static void main(String[] args) { public static void main(String[] args) {
Set<Map<String, String>> demo = readCSV("demo.csv"); Set<Map<String, String>> demo = readCSV("demo.csv");
...@@ -155,34 +174,4 @@ public class CSVReaderWriter { ...@@ -155,34 +174,4 @@ public class CSVReaderWriter {
System.out.println(success ? "Wrote file!" : "Didn't write file"); 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"));
}
}
} }
A,B,C,D A,B,C,D
larry,curly,moe,
one,two,,three one,two,,three
four,,five,six four,,five,six
7,8,9, 7,8,9,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment