Java 属性:.properties 文件与 xml? [英] Java properties: .properties files vs xml?
问题描述
我是属性的新手,我读到 XML 是存储这些属性的首选方式.但是,我注意到,以
I'm a newbie when it comes to properties, and I read that XML is the preferred way to store these. I noticed however, that writing a regular .properties file in the style of
foo=bar
fu=baz
也有效.这意味着打字会少很多(也许更容易阅读,也更高效).那么使用 XML 文件有什么好处?
also works. This would mean a lot less typing (and maybe easier to read and more efficient as well). So what are the benefits of using an XML file?
推荐答案
在 XML 中,您可以存储比在属性文件中更复杂(例如分层)的数据.所以这取决于你的用例.如果您只想存储少量直接属性,则属性文件更容易处理(尽管 Java 属性类也可以读取基于 XML 的属性).
In XML you can store more complex (e.g. hierarchical) data than in a properties file. So it depends on your usecase. If you just want to store a small number of direct properties a properties file is easier to handle (though the Java properties class can read XML based properties, too).
无论如何保持您的配置界面尽可能通用是有意义的,因此您可以毫无问题地切换到另一种表示形式(例如通过使用 Apache Commons 配置 ) 如果需要.
It would make sense to keep your configuration interface as generic as possible anyway, so you have no problem to switch to another representation ( e.g. by using Apache Commons Configuration ) if you need to.
这篇关于Java 属性:.properties 文件与 xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!