A completely re-written, Open Source JavaProperties class is now available in Kajabity Tools and available under the Apache License, Version 2.0.
I’m working on a little personal project to write a GUI editor for some specialised data files – It’s taking longer than I hoped which is why I’ve not Posted for a while. One hurdle is that I need to use a lot of reference data which is currently available in Java style “.properties” files. This is itself a specialised Java specific file format – popular in the Java world but, unsurprisingly, not supported by C#.
I had a quick hunt around the internet and came across a couple of links suggesting very simple ways to achieve something similar (such as this one).
Unfortunately, the “.properties” format is significantly more complex than this suggests and supports a broad range of syntax elements to handle comments and non-ascii characters – and to embed spaces and equals in the keys. Take heart, C# programmers though – one Java programmer had added the line “Hong Kong = Hong Kong” in a language properties file – not realising that the property name here is “Hong” and the property value is “Kong = Hong Kong”. The correct property should be written “Hong Kong = Hong Kong” (escape the first space).
So, to ensure I didn’t have any problems if the properties files I used were anything but basic, I thought I better implement something a bit more capable. The obvious solution is to implement a C# class based on the existing Java version – so this I have done, resulting in the
Kajabity.Tools.JavaProperties class (
Download Source ZIP (removed): JavaProperties.zip. The use of this software is entirely at your own risk.
This version uses C# friendly names and implements the most important features of the Java Properties class (as far as I can see) – including the Load and Store methods. It extends the System.Collections.Hashtable class and all of it’s features are available. I have improved slightly on the original and it should be more robust – for example when non-strings are added to the class I use the C# Object.ToString() to convert them to strings.
The zip also includes some sample “.properties” files to illustrate the features – and to show why a simpler parser won’t suffice.
Hope you find this useful.
9 comments on “Loading Java Properties Files in C#”
The class is pretty nice but looks like there is a defect! – if I have property
website = http://en.wikipedia.org/
it comes to (after loading and storing file with one) –
I believe extra backslash is redundant here. What do you think?
Yes, you are correct – it does behave as you say.
But so does the original Java version.
The C# load and store code is simply a translation of the existing Java code – with a few class level changes so it should behave identically.
Hi, Do I consider this code as public domain? For use in commercial software?
It is from my point of view – but since it is based on Java code I have sent an email to Sun just to check. As soon as they reply, I will put a suitable licence on it.
An update for both Santosh and Yuriy – the rewritten version of the JavaProperties class in Kajabity Tools v0.1 is released as Open Source (Sun never replied…) under the Apache Licence, Version 2.0 – and I have attempted to ensure it only escapes characters where necessary.
Hope that helps.
JavaProperties class is gold 🙂
hey look I have the following problem,
I am novice and do not take your Kajabity.Tools.Java as my request is if I can make a Escript of example only to see its structure
hopefully I can answer my mail or in this forum
the download link is not there anymore 🙁
Yes – as I say at the top of the article, I’ve moved it.
The new version is in Kajabity Tools – have a look at: http://www.kajabity.com/content/kajabity-tools/
The source is also available on GitHub at https://github.com/Kajabity/Kajabity-Tools