How to write bean to CSV using OpenCSV without converting to String[]?
Solution 1
You call writeNext()
in a loop, converting one bean at a time. Not memory-expensive at all.
Solution 2
Jobin, seriously, if you are using OpenCSV then use the BeanToCsv
or the newer StatefulBeanToCsv
. It will save your sanity.
private static void writeRowsToCsv(Path filePath, List<MyBean> rows)
throws IOException {
StringWriter writer = new StringWriter();
ColumnPositionMappingStrategy mappingStrategy =
new ColumnPositionMappingStrategy();
mappingStrategy.setType(MyBean.class);
StatefulBeanToCsvBuilder<MyBean> builder = new StatefulBeanToCsvBuilder(writer);
StatefulBeanToCsv beanWriter = builder
.withMappingStrategy(mappingStrategy)
.withSeparator('#')
.withQuotechar('\'')
.build();
beanWriter.write(rows);
}
jobin
Updated on June 14, 2022Comments
-
jobin almost 2 years
I have a large array of a bean that needs to be written to a CSV file. I am trying to use OpenCSV but all the examples I see on the web ask me to convert the bean (POJO) that I have to a string array. I don't want to do this since this would mean I will have to go through the million beans and convert each of those beans to String[] which would be a very memory-expensive process.
This is the piece of code I have right now for doing this:
private static void writeRowsToCsv(Path filePath, List<MyBean> rows) throws IOException { StringWriter writer = new StringWriter(); CSVWriter csvWriter = new CSVWriter(writer, '#', '\''); ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy(); mappingStrategy.setType(MyBean.class); List<String[]> rowsInStringArray = convertBeanToStringArray(rows) csvWriter.writeAll(rowsInStringArray); }
Is there a way I can avoid the conversion to
String[]
and use the list of beans that I have to write to a CSV file? -
ledlogic over 6 yearsThe constructor StatefulBeanToCsvBuilder(CSVWriter) does not exist.
-
Scott Conway over 6 yearsledlogic - you are absolutely correct. I corrected my code example above as the StatefulBeanToCsv creates a CSVWriter based on values passed in from the builder but it does not take a CSVWriter directly. Not exactly sure what I was thinking back in May but thank you for pointing that out because who knows how long that would have stayed there.
-
Bharath over 6 yearsthis is the easier way to do it.