Reputation: 1211
I am trying to generated a CSV where i can use custom separator other than comma which is colon(:), which means while generating the csv if a string contains colon(:) then it should be appending double quotes around them.I have used apache commons api but i was unable to solve this purpose.I came across a new api which is super csv , i am trying using that to generate csv ,below is the code i have tried
public class SuperCsvWriter{
public static void main(String ar[]){
List<Employee> emps = generateDemoData();
StringWriter writer = new StringWriter();
ICsvBeanWriter beanWriter = new CsvBeanWriter(writer,CsvPreference.STANDARD_PREFERENCE);
final String[] header = new String[] {"id:","name:","age:","Country"};
final CellProcessor[] processors = getProcessors();
beanWriter.writeHeader(header);
for(Employee emp: emps){
beanWriter.write(emp,header,processors);
}
beanWriter.close();
Sytem.out.println("CSV Data\n"+writer.toString());
}
private static CellProcessor[] getProcessors(){
final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(),//ID
new NotNull(), //Name
new Optional(),//Age
new Optional() //Country
};
return processors;
}
private static List<Employee> generateDemoData(){
List<Employee> empsToAdd = new ArrayList<Employee>();
Employee emp = new Employee();
emp.setId("1");
emp.setName("Pankaj, saha");
emp.setAge(":30");
Employee emp1 = new Employee();
emp1.setId("2");
emp1.setName("Timber:Hups");
emp1.setAge(":10");
emp1.setCountry("USA");
empsToAdd.add(emp);
empsToAdd.add(emp1);
return empToAdd;
My bean class
public class Employee{
private String id;
private String name;
private String age;
private String country;
..........................
Now my requirement is whenever csv will be generated it should print like below
"id:","name:","age:",country
1,"pankaj,saha",":30",
2,"Timber:Hups",":10",USA
But here i am getting exception
Exception in thread "main" org.supercsv.exception.SuperCsvReflectionException: unable to find
greater for field id: in class com.test.Employee - check that the context = null
Not sure why i need to add colon in the bean class , as well as what is the to achieve the custom separator using super csv
Upvotes: -1
Views: 1430
Reputation: 807
This is happening because your column headers are not matching with name of the field.
Change
final String[] header = new String[] {"id:","name:","age:","Country"};
to
final String[] header = new String[] {"id","name","age","Country"};
For custom separator to work, you need specify the same using CsvPreference
CsvPreference csvPreference = new CsvPreference.Builder('"', ':', "\r\n").build();
ICsvBeanWriter beanWriter = new CsvBeanWriter(writer, csvPreference);
Also, you can use Open CSV to generate the same. Not sure what issue you were facing. I am able to generate with colon (:) as a separator. Please find the sample below.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(stream);
com.opencsv.CSVWriter writer = new com.opencsv.CSVWriter(outputStreamWriter,':', ICSVWriter.DEFAULT_QUOTE_CHARACTER,ICSVWriter.DEFAULT_ESCAPE_CHARACTER,ICSVWriter.DEFAULT_LINE_END);
List<Employee> emps = generateDemoData();
for (Employee emp : emps) {
writer.writeNext(new String[]{emp.getName(),emp.getAge(),emp.getCountry()});
}
writer.flush();
//byte[] bytes = stream.toByteArray();
System.out.println("CSV Data\n"+new String(stream.toByteArray()));
Upvotes: 0