Java OpenCSV tutorial to work with CSV files
BeanIO is an open source Java framework for marshaling and marshaling Java beans from a flat file, stream, or simple String object. It is very powerful with support for XML, CSV, delimited and fixed length stream formats, Object binding, filed validation, integration with spring-batch, etc. But there are scenarios where you need to simply read or write CSV data, OpenCSV framework is quite useful. Here are simple step by step guide to convert Java objects to CSV data.
Step 1: Define in the maven pom.xml file to bring in the OpenCSV jar file.
<properties> <opencsv.version>2.3</opencsv.version> </properties> <dependencies> <!-- Open CSV --> <dependency> <groupId>net.sf.opencsv</groupId> <artifactId>opencsv</artifactId> <version>${opencsv.version}</version> </dependency> <dependencies>
Step 2: Write the Person model class.
package com.mycompany.app2; public class Person { private String firstName; private String surname; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Step 3: Write the interface.
package com.mycompany.app2; import java.util.List; public interface SimpleWriter { abstract String writeCSV(Listpersons); }
Step 4: Write the implementation using the above interface.
package com.mycompany.app2; import au.com.bytecode.opencsv.CSVWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class SimpleWriterImpl implements SimpleWriter { private static final char DELIMITER = ','; public String writeCSV(List<Person> persons) { StringWriter writer = new StringWriter(); CSVWriter csvWriter = null; try { csvWriter = new CSVWriter(writer, DELIMITER, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, "\n"); List<String[]> records = new ArrayList<String[]>(10); for (Person person : persons) { List<String> record = Arrays.asList(String.valueOf(person.getFirstName()), String.valueOf(person.getSurname()), String.valueOf(person.getAge())); String[] recordArray = new String[record.size()]; record.toArray(recordArray); records.add(recordArray); } csvWriter.writeAll(records); } catch (Exception e) { e.printStackTrace(); } return writer.toString(); } }
If you want to write to a file, you can use a FileWriter instead of the StringWriter.
Step 5: Write a test class to test it.In practice, a unit test class must be written.
package com.mycompany.app2; import java.util.ArrayList; import java.util.List; public class SimpleWriterTest { public static void main(String[] args) { Person p1 = new Person(); p1.setFirstName("John"); p1.setSurname("Smith"); p1.setAge(12); Listpersons = new ArrayList (5); persons.add(p1); SimpleWriter sw = new SimpleWriterImpl(); String csvOutPut = sw.writeCSV(persons); System.out.println(csvOutPut); } }
Step 6: The output is
"John","Smith","12"
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home