I am working on a project using Maven, and I was a little confused on what the right way to store files for access in this program would be?
I have looked at using Maven resources, but I am not super experienced with Maven and it seems like it could be difficult to make regular create/update/delete actions to this folder.
I am currently using java.io & openCSV, but don't know if this is the recommended solution.
This is an example of how I would write to a csv:
public void writeCSV(){
File file = new File("\\example.csv");
String[] row1 = {"101","Joe","Joe@email.com"}
String[] row2 = {"102","Ben","Ben@email.com"}
String[] row3 = {"103","Jill","Jill@email.com"}
String[][] csvRows = new String[3][];
csvRows[0] = row1;
csvRows[1] = row2;
csvRows[2] = row3;
String[] headers = {"id", "name","email"};
csvOut(headers,csvRows,file);
}
private void csvOut(String[] headers, String[][] rows, File file){
FileWriter fw = new FileWriter(file);
CSVWriter cw = new CSVWriter(fw);
cw.writeNext(headers);
for(String[] row : rows){
cw.writeNext(row);
}
cw.close();
fw.close();
}
This is my method for deleting some of the data files:
public void deleteSaveData(){
String workingDir = System.getProperty("user.dir");
File datDir = new File(workingDir+"\\data");
File csvDir = new File(workingDir+"\\csv");
for(File dat: datDir.listFiles((File f) -> f.getName().endsWith("dat"))){
dat.delete();
}
for(File csv: csvDir.listFiles((File f) -> f.getName().endsWith("csv"))){
csv.delete();
}
}
This seems to work as of right now, but I don't know if this would break once exported as a jar to other systems. Would it be advised to rewrite this with the Maven method, or maybe there is some other better standard?
Any advice welcome!
Edit: I am specifically looking for how I should save and read files during run-time.
You should not use resources as those end up on the classpath which for all practical purposes is read-only.
Use regular files or a database depending on your needs.