Question

I have some questions regarding reading and writing to CSV files (or if there is a simpler alternative).

Scenario: I need to have a simple database of people and some basic information about them. I need to be able to add new entries and search through the file for entries. I also need to be able to find an entry and modify it (i.e change their name or fill in a currently empty field).

Now I'm not sure if a CSV reader/writer is the best route or not? I wouldn't know where to begin with SQL in Java but if anyone knows of a good resource for learning that, that would be great.

Currently I am using SuperCSV, I put together a test project based around some example code:

class ReadingObjects {

//  private static UserBean userDB[] = new UserBean[2];
private static ArrayList<UserBean> arrUserDB = new ArrayList<UserBean>();

static final CellProcessor[] userProcessors = new CellProcessor[] {
    new StrMinMax(5, 20),
    new StrMinMax(8, 35),
    new ParseDate("dd/MM/yyyy"),
    new Optional(new ParseInt()),
    null
};

public static void main(String[] args) throws Exception {
    ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
    try {
      final String[] header = inFile.getCSVHeader(true);
      UserBean user;
      int i = 0;
      while( (user = inFile.read(UserBean.class, header, userProcessors)) != null) {
        UserBean addMe = new UserBean(user.getUsername(), user.getPassword(), user.getTown(), user.getDate(), user.getZip());

        arrUserDB.add(addMe);
        i++;

      }
    } finally {
      inFile.close();
    }

    for(UserBean currentUser:arrUserDB){
        if (currentUser.getUsername().equals("Klaus")) {
            System.out.println("Found Klaus! :D");
        }
    }

    WritingMaps.add();
   }
}

And a writer class:

class WritingMaps {

  public static void add() throws Exception { 
ICsvMapWriter writer = new CsvMapWriter(new FileWriter("foo.csv", true), CsvPreference.EXCEL_PREFERENCE);
try {
  final String[] header = new String[] { "username", "password", "date", "zip", "town"};
  String test = System.getProperty("line.seperator");


  // set up some data to write
  final HashMap<String, ? super Object> data1 = new HashMap<String, Object>();
  data1.put(header[0], "Karlasa");
  data1.put(header[1], "fdsfsdfsdfs");
  data1.put(header[2], "17/01/2010");
  data1.put(header[3], 1111);
  data1.put(header[4], "New York");


  System.out.println(data1);

  // the actual writing
//      writer.writeHeader(header);
  writer.write(data1, header);
//      writer.write(data2, header);
} finally {
  writer.close();
}
  }
}

Issues: I'm struggling to get the writer to add a new line to the CSV file. Purely for human readability purposes, not such a big deal. I'm not sure how I would add data to an existing record to modify it. (remove and add it again? Not sure how to do this).

Thanks.

Was it helpful?

Solution

The easiest solution is to read the file at application startup into an in-memory structure (list of UserBean, for example), to add, remove, modify beans in this in-memory structure, and to write the whole list of UserBean to the file when the app closes, or when the user chooses to Save.

Regarding newlines when writing, the javadoc seems to indicate that the writer will take care of that. Just call write for each of your user bean, and the writer will automatically insert newlines between each row.

OTHER TIPS

Have you considered an embedded database like H2, HSQL or SQLite? They can all persist to the filesystem and you'll discover a more flexible datastore with less code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top