Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java properties: .properties files vs xml?

Tags:

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 

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?

like image 224
pg-robban Avatar asked Sep 04 '09 16:09

pg-robban


People also ask

What is the main benefit of using properties file in Java?

The advantage of using properties file is we can configure things which are prone to change over a period of time without the need of changing anything in code. Properties file provide flexibility in terms of configuration. Sample properties file is shown below, which has information in key-value pair.

Where should the properties file in Java?

Write to the properties file The path/to/config. properties is created.

What is the use of .properties file?

properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


2 Answers

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).

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.

like image 171
Daff Avatar answered Nov 17 '22 08:11

Daff


The biggest benefit to using an XML file is that XML declares its encoding, while .properties does not.

If you are translating these properties files to N languages, it is possible that these files could come back in N different encodings. And if you're not careful, you or someone else could irreversibly corrupt the character encodings.

like image 20
Mike Sickler Avatar answered Nov 17 '22 10:11

Mike Sickler