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
No related branches found
No related tags found
No related merge requests found
......@@ -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,7 +167,6 @@ public class CSVReaderWriter {
}
}
}
/*
public static void main(String[] args) {
Set<Map<String, String>> demo = readCSV("demo.csv");
......@@ -155,34 +174,4 @@ public class CSVReaderWriter {
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
larry,curly,moe,
one,two,,three
four,,five,six
7,8,9,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment