views:

247

answers:

5

I'm working on a Java project for class that stores workout information in a flat file. Each file will have the information for one exercise (BenchPress.data) that holds the time (milliseconds since epoch), weight and repetitions.

Example:

1258355921365:245:12

1258355921365:245:10

1258355921365:245:8

What's the most efficient way to store and retrieve this data? It will be graphed and searched through to limit exercises to specific dates or date ranges.

Ideas I had was to write most recent information to the top of the file instead of appending at the end. This way when I start reading from the top, I'll have the most recent information, which will match most of the searches (assumption).

There's no guarantee on the order of the dates, though. A user could enter exercises for today and then go in and enter last weeks exercises, for whatever reason. Should I take the hit upon saving to order all of the information by the date?

Should I go a completely different direction? I know a database would be ideal, but this is a group project and managing a database installation and data synch amongst us all would not be ideal. The others have no experience with databases and it'll make grading difficult.

So thanks for any advice or suggestions.

-John

+1  A: 

Read in every line via BufferedReader and parse with StringTokenizer. Looking at the data, I'd likely store an array of fields in a List that can be iterated and sorted according to your preference.

Xepoch
Why an array of fields? I'd say make a class representing the data and store objects in a list.
ColinD
For providing own sort, best to use an array-like structure as plain List (not ArrayList) would not guarantee field order. I'm suggesting `Collections` because I would assume that's what an instructor would like to see :)
Xepoch
+5  A: 

Don't overcomplicate things. Unless you are dealing with million records you can just read the whole thing into memory and sort it any way you like. And always add records in the end, this way you are less likely to damage your file.

yu_sha
A: 

If you must store the file in this format, you're likely best off just reading the entire thing into memory at startup and storing it in a TreeMap or some other sorted, searchable map. Then you can use TreeMap's convenience methods such as ceilingKey or the similar floorKey to find matches near certain dates/times.

Steven Schlansker
+2  A: 

For simple projects, using an embedded like JavaDB / Apache Derby may be a good idea. Configuration for the DB is absolutely minimal and in your case, you may need a maximum of just 2 tables (User and Workout). Exporting data to file is also fairly simple for sync between team members.

As yu_sha pointed out though, unless expect to have a large dataset ( to run on a PC , > 50000), you can just use the file and read everything into memory.

Thimmayya
+1, using a database helps you avoid breaks when you need to add new columns to the file format.
Harold L
A: 

Use flatworm, a Java library allowing to parse and create flat files. Describe the format with a simple XML definition file, and there you go.

Pascal Thivent