Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write ArrayList<Object> to a csv file

I have a ArrayList<Metadata> and i want to know if there is a Java API for working with CSV files which has a write method which accepts a ArrayList<> as parameter similar to LinqToCsv in .Net. As i know OpenCSV is available but the CsvWriter class doesn't accept a collection. My Metadata Class is

public class Metadata{
    private String page;
    private String document;
    private String loan;
    private String type;
}
ArrayList<Metadata> record = new ArrayList<Metadata>();

once i populate the record, i want to write each row into a csv file. Please suggest.

like image 831
Maverick Avatar asked Jan 28 '16 09:01

Maverick


People also ask

Can you make an ArrayList of objects?

You can simply use add() method to create ArrayList of objects and add it to the ArrayList. This is simplest way to create ArrayList of objects in java.

Can Arraylists store objects?

The Java collection classes, including ArrayList, have one major constraint: they can only store pointers to objects, not primitives. So an ArrayList can store pointers to String objects or Color objects, but an ArrayList cannot store a collection of primitives like int or double.


2 Answers

Surely there'll be a heap of APIs that will do this for you, but why not do it yourself for such a simple case? It will save you a dependency, which is a good thing for any project of any size.

Create a toCsvRow() method in Metadata that joins the strings separated by a comma.

public String toCsvRow() {
    return Stream.of(page, document, loan, type)
            .map(value -> value.replaceAll("\"", "\"\""))
            .map(value -> Stream.of("\"", ",").anyMatch(value::contains) ? "\"" + value + "\"" : value)
            .collect(Collectors.joining(","));
}

Collect the result of this method for every Metadata object separated by a new line.

String recordAsCsv = record.stream()
        .map(Metadata::toCsvRow)
        .collect(Collectors.joining(System.getProperty("line.separator")));

EDIT Should you not be so fortunate as to have Java 8 and the Stream API at your disposal, this would be almost as simple using a traditional List.

public String toCsvRow() {
    String csvRow = "";
    for (String value : Arrays.asList(page, document, loan, type)) {
        String processed = value;
        if (value.contains("\"") || value.contains(",")) {
            processed = "\"" + value.replaceAll("\"", "\"\"") + "\"";
        }
        csvRow += "," + processed;
    }
    return csvRow.substring(1);
}
like image 188
Henrik Avatar answered Oct 09 '22 21:10

Henrik


By using CSVWriter, you could convert the ArrayList to an array, and pass that to the writer .

csvWriter.writeNext(record.toArray(new String[record.size()]));
like image 23
Arnaud Avatar answered Oct 09 '22 20:10

Arnaud