You could pass the list as a string directly into the CSVParser instead of creating a writer.
CSVRecord csvr = CSVParser.parse(
values.stream().collect(Collectors.joining(","))
,csvFormat.withHeader(header.toArray(new String[header.size()])))
.getRecords().get(0);
Answer from Berkley Lamb on Stack OverflowApache Commons
commons.apache.org › proper › commons-csv › jacoco › org.apache.commons.csv › CSVRecord.java.html
CSVRecord.java - Apache Commons
*/ package org.apache.commons.csv; import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; /** * A CSV record parsed from a CSV file.
Java Tips
javatips.net › api › org.apache.commons.csv.csvrecord
Java Examples for org.apache.commons.csv.CSVRecord
public static Collection<Lead> importFile(File f) { try { Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(new FileReader(f)); Iterator<CSVRecord> lines = records.iterator(); if (!lines.hasNext()) return null; CSVRecord headerRow = lines.next(); List<Lead> leads = new ArrayList<>(); while (lines.hasNext()) { final Iterator<String> content = lines.next().iterator(); final Iterator<String> header = headerRow.iterator(); Lead lead = new Lead(null, "new lead"); StringBuilder desc = new StringBuilder(); // use capterra as default campaign since it doesn't reference // itself in the import file C
GitHub
github.com › apache › commons-csv › blob › master › src › main › java › org › apache › commons › csv › CSVRecord.java
commons-csv/src/main/java/org/apache/commons/csv/CSVRecord.java at master · apache/commons-csv
import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; · /** * A CSV record parsed from a CSV file.
Author apache
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVRecord.html
CSVRecord (Apache Commons CSV 1.14.2-SNAPSHOT API)
java.lang.Object · org.apache.commons.csv.CSVRecord · All Implemented Interfaces: Serializable, Iterable<String> public final class CSVRecord extends Object implements Serializable, Iterable<String> A CSV record parsed from a CSV file. Note: Support for Serializable is scheduled to be removed in version 2.0.
CalliCoder
callicoder.com › java-read-write-csv-file-apache-commons-csv
Read / Write CSV files in Java using Apache Commons CSV | CalliCoder
February 18, 2022 - import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths; public class CSVReaderWithManualHeader { private static final String SAMPLE_CSV_FILE_PATH = "./users.csv"; public static void main(String[] args) throws IOException { try ( Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH)); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT .withHeader("Name", "Email", "Phon
Top answer 1 of 2
4
You could pass the list as a string directly into the CSVParser instead of creating a writer.
CSVRecord csvr = CSVParser.parse(
values.stream().collect(Collectors.joining(","))
,csvFormat.withHeader(header.toArray(new String[header.size()])))
.getRecords().get(0);
2 of 2
1
The BeanIO and SimpleFlatMapper are way better at solving this problem. BeanIO uses a Map data structure and a config file to declare how the CSV file should be structured so it is very powerful. SimpleFlatMapper will take you POJO properties as the heading names by default and output the property values are column values.
BeanIO
http://beanio.org/2.1/docs/reference/index.html#CSVStreamFormat
SimpleFlatMapper
http://simpleflatmapper.org/
CsvParser
.mapTo(MyObject.class)
.stream(reader)
.forEach(System.out::println);
GitHub
github.com › kana-ph › csv-viewer › blob › master › src-commons-csv › org › apache › commons › csv › CSVRecord.java
csv-viewer/src-commons-csv/org/apache/commons/csv/CSVRecord.java at master · kana-ph/csv-viewer
import java.util.Map.Entry;
·
· /**
· * A CSV record parsed from a CSV file.
· *
· * @version $Id: CSVRecord.java 1727809 2016-01-31 13:08:33Z sebb $
· */
· public final class CSVRecord implements Serializable, Iterable<String> {
·
· private static final String[] EMPTY_STRING_ARRAY = new String[0];
·
Author kana-ph
Java2s
java2s.com › example › java-api › org › apache › commons › csv › csvrecord › get-1-0.html
Example usage for org.apache.commons.csv CSVRecord get
From source file:bariopendatalab.ImportData.java · /** * @param args the command line arguments *//*from ww w . ja v a 2 s .c o m*/ public static void main(String[] args) { int i = 0; try { MongoClient client = new MongoClient("localhost", 27017); DBAccess dbaccess = new DBAccess(client); dbaccess.dropDB(); dbaccess.createDB(); FileReader reader = new FileReader(new File(args[0])); Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().withIgnoreEmptyLines().withDelimiter(',') .parse(reader); i = 2; for (CSVRecord record : records) { String type = record.get("Tipologia"); if (type == null
Javadoc.io
javadoc.io › doc › org.apache.commons › commons-csv › 1.7 › org › apache › commons › csv › CSVRecord.html
CSVRecord (Apache Commons CSV 1.7 API)
Bookmarks · Latest version of org.apache.commons:commons-csv · https://javadoc.io/doc/org.apache.commons/commons-csv · Current version 1.7 · https://javadoc.io/doc/org.apache.commons/commons-csv/1.7 · package-list path (used for javadoc generation -link option) · https://javadoc.io/d...
Javadoc.io
javadoc.io › doc › org.apache.commons › commons-csv › 1.6 › org › apache › commons › csv › CSVRecord.html
CSVRecord (Apache Commons CSV 1.6 API)
https://javadoc.io/doc/org.apache.commons/commons-csv · Current version 1.6 · https://javadoc.io/doc/org.apache.commons/commons-csv/1.6 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/org.apache.commons/commons-csv/1.6/package-list ·
Dukelearntoprogram
dukelearntoprogram.com › course2 › doc › javadoc › org › apache › commons › csv › CSVRecord.html
CSVRecord
java.lang.Object · org.apache.commons.csv.CSVRecord ·
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
DZone
dzone.com › coding › java › working with csv files in java using apache commons csv
Working With CSV Files in Java Using Apache Commons CSV
April 30, 2018 - import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; public class BasicCsvReader { public static void main(String[] args) throws IOException { BufferedReader reader = Files.newBufferedReader(Paths.get("student.csv")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader("Student Name", "Fees").withIgnoreHeaderCase().withTrim()); for (CSVRecord csvRecord: csvParser) { // Accessing Values by Co
GitHub
github.com › apache › commons-csv › blob › master › src › test › java › org › apache › commons › csv › CSVRecordTest.java
commons-csv/src/test/java/org/apache/commons/csv/CSVRecordTest.java at master · apache/commons-csv
import java.util.concurrent.atomic.AtomicInteger; · import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; · class CSVRecordTest { · private enum EnumFixture { UNKNOWN_COLUMN ·
Author apache
Java Tips
javatips.net › api › jbasics-master › src › main › java › org › jbasics › csv › CSVRecord.java
CSVRecord.java example
*/ package org.jbasics.csv; import ...ner.Indexed; import org.jbasics.utilities.DataUtilities; import java.io.IOException; import java.util.*; @ThreadSafe @ImmutableState public class CSVRecord implements Iterable<String>, Indexed<String> { public final static CSVRecord EMPTY_RECORD ...
Googlesource
apache.googlesource.com › commons-csv › + › a064b9730522840269a98fd009d426068db1307c › src › main › java › org › apache › commons › csv › CSVRecord.java
src/main/java/org/apache/commons/csv/CSVRecord.java - commons-csv - Git at Google
apache / commons-csv / a064b9730522840269a98fd009d426068db1307c / . / src / main / java / org / apache / commons / csv / CSVRecord.java
GitHub
github.com › sujaybhowmick › csv-parser › blob › master › src › main › java › com › optimus › csv › parser › CSVRecord.java
csv-parser/src/main/java/com/optimus/csv/parser/CSVRecord.java at master · sujaybhowmick/csv-parser
import java.util.Map; · /** * Created with IntelliJ IDEA. * User: sujay · * Date: 1/12/14 · * Time: 10:10 AM · * To change this template use File | Settings | File Templates. */ public final class CSVRecord implements Serializable, Iterable<String> { private static final String[] EMPTY_STRING_ARRAY = new String[0]; ·
Author sujaybhowmick
Opencrx
opencrx.org › opencrx › 3.0 › java › org › opencrx › application › uses › org › apache › commons › csv › CSVRecord.html
CSVRecord (openCRX/Core API)
java.lang.Object · org.opencrx.application.uses.org.apache.commons.csv.CSVRecord · All Implemented Interfaces: Serializable, Iterable<String> public class CSVRecord extends Object implements Serializable, Iterable<String> A CSV record · Version: $Id: $ See Also: Serialized Form ·