Java:从文件存储/检索锻炼信息的最有效方法?

发布于 2024-08-11 23:34:39 字数 539 浏览 8 评论 0原文

我正在开发一个用于班级的 Java 项目,该项目将锻炼信息存储在平面文件中。每个文件将包含一项练习的信息 (BenchPress.data),其中包含时间(自纪元以来的毫秒数)、重量和重复次数。

示例:

1258355921365:245:12

1258355921365:245:10

1258355921365:245:8

存储和检索此数据的最有效方法是什么?它将被绘制成图表并进行搜索,以将练习限制在特定日期或日期范围内。

我的想法是将最新信息写入文件顶部而不是附加在末尾。这样,当我从顶部开始阅读时,我将获得最新的信息,该信息将与大多数搜索相匹配(假设)。

不过,无法保证日期的顺序。用户可以出于任何原因输入今天的练习,然后进入并输入上周的练习。我是否应该在保存后立即按日期订购所有信息?

我应该走完全不同的方向吗?我知道数据库是理想的,但这是一个小组项目,在我们所有人之间管理数据库安装和数据同步并不理想。其他人没有数据库经验,这会让评分变得困难。

所以感谢您的任何意见或建议。

-约翰

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌吟 2024-08-18 23:34:39

不要让事情过于复杂化。除非您正在处理数百万条记录,否则您可以将整个记录读入内存并按照您喜欢的方式对其进行排序。并且始终在最后添加记录,这样您就不太可能损坏文件。

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.

扎心 2024-08-18 23:34:39

对于简单的项目,使用像 JavaDB / Apache Derby 可能是个好主意。数据库的配置绝对是最少的,在您的情况下,您可能最多只需要 2 个表(用户和锻炼)。将数据导出到文件对于团队成员之间的同步也相当简单。

正如 yu_sha 指出的那样,除非期望有一个大的数据集(在 PC 上运行,> 50000),否则您可以只使用该文件并将所有内容读入内存。

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.

£冰雨忧蓝° 2024-08-18 23:34:39

通过 BufferedReader 读取每一行并使用 StringTokenizer 进行解析。查看数据,我可能会将字段数组存储在 List 中,可以根据您的偏好对其进行迭代和排序。

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.

澜川若宁 2024-08-18 23:34:39

如果必须以这种格式存储文件,那么最好在启动时将整个文件读入内存并将其存储在 TreeMap 或其他一些排序的可搜索映射中。然后您可以使用 TreeMap 的便捷方法,例如 ceilingKey 或类似的 FloorKey 可查找特定日期/时间附近的匹配项。

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.

恋你朝朝暮暮 2024-08-18 23:34:39

使用 flatworm,这是一个允许解析和创建平面文件的 Java 库。使用简单的 XML 定义文件描述格式,然后就可以了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文