java - How do I append more record into existing .csv file -


i trying write .csv file using java.i using supercsv jar. have scenario have write record batch. example if have 100 record have write same .csv in batches of ten.

  1. first problem facing not giving me option download file or not writing csv file. getting value in console.
  2. how achieve writing batches.

this java code

 public class supercsvparserexample {   public static void main(string[] args) throws ioexception {       //list<employee> emps = readcsvtobean();      employee emp1=new employee();      employee emp2=new employee();      employee emp3=new employee();      employee emp4=new employee();      employee emp5=new employee();      emp1.setid("1");      emp1.setname("a");      emp1.setrole("developer");      emp1.setsalary("100000");       emp2.setid("2");      emp2.setname("b");      emp2.setrole("tester");      emp2.setsalary("200000");       emp3.setid("3");      emp3.setname("c");      emp3.setrole("developer");      emp3.setsalary("300000");       emp4.setid("4");      emp4.setname("d");      emp4.setrole("developer");      emp4.setsalary("500000");       emp5.setid("5");      emp5.setname("e");      emp5.setrole("manager");      emp5.setsalary("600000");       list<employee>emps=new arraylist<employee>();         emps.add(emp1);         emps.add(emp2);         emps.add(emp3);         emps.add(emp4);         emps.add(emp5);         system.out.println(emps);           system.out.println(emps);         system.out.println("******");         writecsvdata(emps);     }      private static void writecsvdata(list<employee> emps) throws ioexception {         icsvbeanwriter beanwriter = null;         stringwriter writer = new stringwriter();         try{             beanwriter = new csvbeanwriter(new filewriter("c:/users/user/desktop/employees.cs"),csvpreference.standard_preference);             final string[] header = new string[]{"id","name","role","salary"};             final cellprocessor[] processors = getprocessors();              // write header             beanwriter.writeheader(header);              //write beans data             for(employee emp : emps){                 beanwriter.write(emp, header, processors);             }         }finally{             if( beanwriter != null ) {                 beanwriter.close();             }         }         system.out.println("csv data\n"+writer.tostring());     }      private static list<employee> readcsvtobean() throws ioexception {         icsvbeanreader beanreader = null;         list<employee> emps = new arraylist<employee>();         try {             beanreader = new csvbeanreader(new filereader("employees.csv"),                     csvpreference.standard_preference);              // name mapping provide basis bean setters              final string[] namemapping = new string[]{"id","name","role","salary"};             //just read header, don't mapped employee object             final string[] header = beanreader.getheader(true);             final cellprocessor[] processors = getprocessors();              employee emp;              while ((emp = beanreader.read(employee.class, namemapping,                     processors)) != null) {                 emps.add(emp);             }          } {             if (beanreader != null) {                 beanreader.close();             }         }         return emps;     }      private static cellprocessor[] getprocessors() {          final cellprocessor[] processors = new cellprocessor[] {                  new uniquehashcode(), // id (must unique)                 new notnull(), // name                 new optional(), // role                 new notnull() // salary         };         return processors;     }  } 


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -