views:

132

answers:

3

I want to make a GUI application that contains three functions as follows:

  • Add a record
  • Edit a record
  • Delete a record

A record contains two fields - Name and Profession

There are two restrictions for the application

  • You can't use database to store info. You have to use a flat file.
  • Total file should not be re-written for every add/delete operation.

So, my questions are mentioned below:

Q1. Which file format would be better? (.xml or .csv or .txt or any other)

Q2. How can we perform the add/delete operation without the whole file being re-written?

A: 

I would go with CSV, zipped. it is both readable, and editable externally.

If CSV is your choice, this can help: http://javacsv.sourceforge.net/

Did you look at this? http://sourceforge.net/projects/flatworm/ Also consider Apache Derbi and HSQLDB

Another solution is this http://www.coyotegulch.com/products/jisp/index.html

You can reinvent the wheel, but that is only required if this is an academic assigment...

Ehrann Mehdan
A: 

Given that the whole file must not be rewritten, I would suggest using RandomAccessFile that allow you to read and write only the record you want.

For the file format, a binary file, using fixed length for the record : ex: Name on 20 characters, Profession on 30. This will allow you to use the seek() method of RandomAccessFile to directly access your data.

chburd
+2  A: 

The second part of your question is answered here : Best Way to Write Bytes in the Middle of a File in Java

As for the format - I would go with something as simple as possible. You don't want to have to deal with a bunch of markup processing, as using RandomAccessFile, you will going directly to a byte position. A fixed width style format would be good, so that based on the record number, you can calculate the starting position of a record or field in the file, without having to read everything in the file. The fields would then be padded out to the fixed width with spaces or some other suitable character.

jason