-1

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

Mandrek
  • 1,159
  • 6
  • 25
  • 55

1 Answers1

0

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()));
Pramod
  • 787
  • 4
  • 19
  • i am getting this CSV Data "Pankaj, saha":":30": "Timber:Hups":":10":"USA", i am getting : instead comma – Mandrek Jul 18 '21 at 14:18
  • Isn't that what you want? Your question says `where i can use custom separator other than comma which is colon(:)`. If you want to keep comma as a delimiter, then continue using standard csv preference `CsvPreference.STANDARD_PREFERENCE ` – Pramod Jul 18 '21 at 20:40
  • Pankaj, saha",":30", "Timber:Hups",":10","USA" => This format i want – Mandrek Jul 19 '21 at 09:11
  • If you use CsvPreference.STANDARD_PREFERENCE, you would get as below. `id,name,age,Country` `1,"Pankaj, saha",:30,` `2,Timber:Hups,:10,USA` – Pramod Jul 19 '21 at 09:38
  • I want Pankaj, saha",":30", "Timber:Hups",":10","USA" => which means it will behave like normal csv generator additionally when it will encounter : it will surround it with double quotes – Mandrek Jul 19 '21 at 09:40