tldr: I'm new to programs with persistent data and am looking for the right way to save/load files. Can anyone point me in the right direction?
I undertaking a project which involves much saving and loading of files between two related systems. Simplified, there is one program which functions as the 'builder' for developers and another which is the 'interface' for clients.
In the 'builder', developers have the functionality to define Items and their possible properties which clients may interact with in the interface. For example, the developer may make an Item called 'car' which may have colour, model, and speed properties; and also an item called 'house' which has dimensions, colour. He can make any number of items, which may be diverse and each will have a few comm开发者_StackOverflowon properties (name, colour) and also an array of different properties.
In the 'interface', clients may choose any number of items into their world. Ie, they may take 2 cars and 1 house, and separately define their properties such that one of the cars is a Fast Red Hotrod, another is a Slow White Van, and the house is Brown and 3x2.
both Car and House are instances of Item, but the client has further specified 2 different instances of Car.
Currently I have saving the different instances (car, house etc) into text files and this seems to be working well, but I'm sure there must be a better way (particularly as I intend the client interface to be runnable on Android). Can anyone point me in the right direction (s) or to some material which would help me make the choice?
One option is to use Java binary serialization. I would personally avoid that option.
There are plenty of other serialization frameworks around. For example, I've ported Google's Protocol Buffers framework to C#, so I'm generally biased in favour of that - but there are lots of other options too, such as:
- XML
- JSON
- Thrift
These options generally have advantages in terms of:
- Portability to other platforms (consider if you want to build an iPhone or Windows Phone 7 app in the future)
- Backward and forward compatibility
- Human readability (or easy conversion to a human format)
- Some of them are very compact, too
Admittedly Java binary serialization makes the easy path very easy when it's working - but it can be a pain in other ways. Just my experience.
Databases are excellent places to put persistent data.
You can use Serialization. You'll need to check how you'll access the file/convert on Android.
精彩评论