java - How to write ArrayList<Object> to a csv file -


i have arraylist<metadata> , want know if there java api working csv files has write method accepts arraylist<> parameter similar linqtocsv in .net. know opencsv available csvwriter class doesn't accept collection. metadata class

public class metadata{     private string page;     private string document;     private string loan;     private string type; } 
arraylist<metadata> record = new arraylist<metadata>(); 

once populate record, want write each row csv file. please suggest.

surely there'll heap of apis you, why not such simple case? save dependency, thing project of size.

create tocsvrow() method in metadata joins strings separated 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 result of method every metadata object separated new line.

string recordascsv = record.stream()         .map(metadata::tocsvrow)         .collect(collectors.joining(system.getproperty("line.separator"))); 

edit should not fortunate have java 8 , stream api @ disposal, simple using traditional list.

public string tocsvrow() {     string csvrow = "";     (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); } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -