Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csv_io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
csv_io
Commits
4821aa8f
Commit
4821aa8f
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Added reader that returns List, for preserving row order
parent
63ffe1e7
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+26
-37
26 additions, 37 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
src/main/resources/csv/demo.csv
+1
-0
1 addition, 0 deletions
src/main/resources/csv/demo.csv
with
27 additions
and
37 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+
26
−
37
View file @
4821aa8f
...
...
@@ -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"
));
}
}
}
This diff is collapsed.
Click to expand it.
src/main/resources/csv/demo.csv
+
1
−
0
View file @
4821aa8f
A,B,C,D
larry,curly,moe,
one,two,,three
four,,five,six
7,8,9,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment