given is an existing CSV file with the following example structure/content:
id,password,role
user1,1234,student
user2,1234,professor
I want to create a basic DAO with simple CRUD operations in Java to access and modify that data. Is it possible to update/edit a single record/line? Or do I have to completely parse and rewrite the 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 and kind regards
Philipp