开发者

Are properties the best way to get specific parameters and data into a java program?

I can understand using properties for messages and international settings but I feel there has to be a better way to get specific parameters of a program like background color, resolution, floating point numbers, etc.

Every time I load a set of properties I have to have a wrapper class to convert all those into something meaningful to my program. Say I want to load a color, I have to read the color as a string and then convert it to a color in my program. Everything in a property is a string. So numbers have to get converted too.

I feel like there should be some sortof of library that can detect(or maybe it's specified in the file) what it is and you would automatically get the correct da开发者_如何学JAVAta type when you load it in.

Is there anything like this? Thanks.


You could use XMLEncoder/XMLDecoder. They are text-based serialize alternatives for java beans.


As KitsuneYMG mentioned, you can use an XML deserializer to more easily handle the unmarshalling of configurable, typed values; XStream is an excellent library to simplify the object field mapping process. Also consider the YAML and JSON serialization formats as alternatives to XML, they are more concise and potentially easier to manage and both have fine Java bindings.

Moreover, by accessing these configurable objects via the Factory pattern you can have better control for loading special cases (such as automated tests) as well as XML documents from the filesystem or classloader resources.


Why is there no reference on the preference api? I believe using XmlEncoder or any other serialization mechanism is a round about way of doing it. I use java preference api for storing custom application specific info. The only problem was there is no default putObject and getObject method.But there is a good workaround. See this link for on how to store objects in preference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜