java - Create DAO for existing CSV file -


given existing csv file following example structure/content:

id,password,role user1,1234,student user2,1234,professor 

i want create basic dao simple crud operations in java access , modify data. possible update/edit single record/line? or have parse , rewrite file?

current implementation:

package com.testproject.persistence;  import java.io.filewriter; import java.io.ioexception; import java.util.list;  import com.testproject.model.user;  public class userdao {     private final static string csv_file = "/users/someuser/desktop/test.csv";      /**      *       * @param user      */     public void add(user user) {         try {             filewriter writer = new filewriter(csv_file, true);              writer.append(user.getid());             writer.append(';');             writer.append(user.getpassword());             writer.append('\n');              writer.flush();             writer.close();         } catch (ioexception e) {             e.printstacktrace();         }     }      /**      *       * @param user      */     public void update(user user) {         //     }      /**      *       * @param userid      */     public void delete(string userid) {         //     }      /**      *       * @return      */     public list<user> findall() {         return null;     }      /**      *       * @param userid      * @return      */     public user findbyprimarykey(string userid) {         return null;     } } 

thanks , kind regards

philipp

the approach: read original csv -> change content in memory , write new file-> delete old file -> rename new file)

two approaches achieve this:

first: if csv not big 1. create object representing each line of csv 2. represent csv collection of objects after reading csv

create: add object in collection , re-write file

read : simple read collection collection replica of csv

update: update object in collection , re-write csv

delete : delete object collection , re-write csv

**the benefit of approach reads free file read.

second: if csv big , have memory constraints, avoiding creating collection still have re-write file update it.

** benefit of approach save memory reads slow


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -