Java Programming - Best way to save/read data for

2020-07-12 10:57发布

Please forgive this quesiton if it has already been answered, but I have been searching online for the past hour to try to figure it out.

I'm working on getting back into Java programming and I'm trying to understand the best way to save/read data I would be storing in my application. Would I put everything into a text file and read it all in when the application starts and save it all on exit? I figured that might eat up too much memory if the application was too large?

3条回答
beautiful°
2楼-- · 2020-07-12 11:30

It depends on how much data you have.

When you want to store a handfull of key/value pairs like some user preferences, you could use the Preferences class, which is implemented using the registry on Windows, config files on Linux and whatever else is the preferred way to store user preferences on other plattforms.

When you have actual data which is too much for the registry but not enough to warrant a database (something in the order of a few MB), using one or more flat files might be a solution. When the data is complex, it might be a good idea to use a standardized format like XML instead of something homebrewed. Java has classes which allow easy parsing and serialization of XML. An alternative quick&dirty solution would be to use ObjectStreams to save and restore whole objects. This is very easy to implement, but might not be very efficient because it stores a lot of meta-information which is likely unnecessary.

But when you have a lot of data (more than you are comfortable to read and write completely), it might be a smart move to use a database. A database allows you easy access to huge amounts of data (in the orders of several GB) and offers you a lot of features for free which would be hard to implement yourself (like searching for records using indices). Databases aren't magic. They also use files to store their data (files with very clever structure, however). You could replicate every feature of a database yourself. But why should you reinvent the wheel when you can just use an existing solution?

查看更多
放荡不羁爱自由
3楼-- · 2020-07-12 11:33

I would say if you need to store few information you can use a properties file.

If you need to store more complex structure than just properties I suggest using a database (HSQL, Oracle, Mysql, MSSQL, etc...). Depending of you needs.

查看更多
forever°为你锁心
4楼-- · 2020-07-12 11:35

If the question is also how to get the data into and out of the file, I just wanted to throw out the keyword "serialization", in case you're not familiar with it. Google it, there's tons of tutorials. In a nutshell, Java has built in support for writing objects to and reading objects from binary bytestreams, which you can then of course write to / read from files. So you won't have to implement the code for storing and reading your different members. Just identify the objects in your code that hold program state and serialize those. You can also serialize to XML making the created files manually editable.

查看更多
登录 后发表回答