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
00284940
Commit
00284940
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
placeCSVonStream() now correctly leaves content on the stream
parent
19ad5c18
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
+6
-5
6 additions, 5 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
+1
-1
1 addition, 1 deletion
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
with
7 additions
and
6 deletions
src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+
6
−
5
View file @
00284940
/*
/*
* CSV Reader/Writer, copyright (c) 2019 Christopher A. Bohn, bohn@unl.edu.
* CSV Reader/Writer, copyright (c) 2019
-2020
Christopher A. Bohn, bohn@unl.edu.
*/
*/
package
edu.unl.cse.csv_io
;
package
edu.unl.cse.csv_io
;
import
com.opencsv.CSVReaderHeaderAware
;
import
com.opencsv.CSVReaderHeaderAware
;
import
com.opencsv.CSVReaderHeaderAwareBuilder
;
import
com.opencsv.CSVReaderHeaderAwareBuilder
;
import
com.opencsv.CSVWriterBuilder
;
import
com.opencsv.CSVWriter
;
import
com.opencsv.ICSVWriter
;
import
com.opencsv.exceptions.CsvValidationException
;
import
com.opencsv.exceptions.CsvValidationException
;
import
java.io.*
;
import
java.io.*
;
...
@@ -86,8 +85,9 @@ public class CSVReaderWriter {
...
@@ -86,8 +85,9 @@ public class CSVReaderWriter {
return
wroteFile
;
return
wroteFile
;
}
}
static
void
placeCSVonStream
(
Collection
<
Map
<
String
,
String
>>
data
,
OutputStream
outputStream
)
{
static
void
placeCSVonStream
(
Collection
<
Map
<
String
,
String
>>
data
,
OutputStream
outputStream
)
throws
IOException
{
ICSVWriter
csvWriter
=
new
CSVWriterBuilder
(
new
OutputStreamWriter
(
outputStream
)).
build
();
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
outputStream
);
CSVWriter
csvWriter
=
new
CSVWriter
(
writer
);
List
<
String
[]>
allLines
=
new
LinkedList
<>();
List
<
String
[]>
allLines
=
new
LinkedList
<>();
String
[]
fieldNames
=
getFieldNames
(
data
);
String
[]
fieldNames
=
getFieldNames
(
data
);
int
number_of_fields
=
fieldNames
.
length
;
int
number_of_fields
=
fieldNames
.
length
;
...
@@ -102,6 +102,7 @@ public class CSVReaderWriter {
...
@@ -102,6 +102,7 @@ public class CSVReaderWriter {
allLines
.
add
(
values
);
allLines
.
add
(
values
);
}
}
csvWriter
.
writeAll
(
allLines
,
false
);
csvWriter
.
writeAll
(
allLines
,
false
);
writer
.
close
();
}
}
private
static
String
[]
getFieldNames
(
Collection
<
Map
<
String
,
String
>>
data
)
{
private
static
String
[]
getFieldNames
(
Collection
<
Map
<
String
,
String
>>
data
)
{
...
...
This diff is collapsed.
Click to expand it.
src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java
+
1
−
1
View file @
00284940
...
@@ -128,7 +128,7 @@ public class CSVReaderWriterTest {
...
@@ -128,7 +128,7 @@ public class CSVReaderWriterTest {
// We probably should test malformed CSVs, but this is good enough for students' starter code
// We probably should test malformed CSVs, but this is good enough for students' starter code
@Test
@Test
public
void
testWriting2x3CSV
()
{
public
void
testWriting2x3CSV
()
throws
IOException
{
// Input
// Input
String
[]
headers
=
{
"header1"
,
"header2"
};
String
[]
headers
=
{
"header1"
,
"header2"
};
String
[][]
rows
=
{{
"datum1"
,
"datum2"
},
{
"datum3"
,
"datum4"
}};
String
[][]
rows
=
{{
"datum1"
,
"datum2"
},
{
"datum3"
,
"datum4"
}};
...
...
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