开发者

Best Config File for Java

I am currently writing an application that works in both JSP and the Andriod platform. I was reading an article on PHP config files and XML files were the fastest(outside the ini).

Its actually this article here: http://www.phpro.org/articles/Application-Configuration.html

So I was wondering if the same would hold true for Java? Or woul开发者_如何学Cd a solution like this be better: http://www.bartbusschots.ie/blog/?p=360 ?


Java (and Android) offers the java.util.Properties API for this. It's much like ini files, with key=value lines. E.g.

filename.properties (just drop file in classpath)

somekey = somevalue
otherkey = othervalue

with

Properties properties = new Properties();
properties.load(SomeClass.class.getResourceAsStream("/filename.properties"));
String somekey = properties.getProperty("somekey"); // somevalue
// ...

See also:

  • The Java Tutorials - Essential Classes - Properties


I would advice you go for the latter (.properties files) as they are very well supported and idiomatic in the Java world. I would only consider using XML if the kind of configuration you wish to represent is very complex and can't be represented effectively (in a readable manner) using plain key=value pairs.


You might want to check out Apache Commons Configuration if you haven't already. It supports many formats like properties and XML files. Even if you just use it for properties style files it has some additional features that can be nice, see here. Things like includes, lists, and variable interpolation.

Unless your configuration is really huge and complex I doubt performance is going to be too huge of a concern, even on an Android device. You should measure it if you believe it might be a problem. Then address it accordingly if it's outside of your desired threshold.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜