load properties file with comments

S

Sanjay

I am wondering whether anybody know a way to load a properties file with
comments.

My problem is that I need to read a sample configuration file (same
format as java properties file) which has many sections in it and each
section has section name and some comments. This properties file is
bundled into a jar file. Then depending on some user input, I will
change some of the properties and then write the file to some other
location. However, I need to keep the format the same, including the
section comments and line breaks before them.

java.util.Properties seems to be a good fit for this purpose to me as I
can use load() and store() methods with pretty much no other code. Is
there a better way to do this? Thanks.

Sanjay
 
S

Stefan Ram

Sanjay said:
I am wondering whether anybody know a way to load a properties
file with comments.

(Slightly off-topic to this question, because I will now
advertise my custom file format instead of property files.
So skip the rest, if you are not interested in it.)

I have specified and implemented a custom notation, which
extends property files (actually S-expressions).

The following example shows how it is read from a string
(I have added three comments):

public class Main
{ public static void main( final java.lang.String[] args )
{
final de.dclj.ram.notation.unotal.RoomSource room =
de.dclj.ram.notation.unotal.RoomFromModule.roomFrom
(
" " +
" < < &car " +
" Chevy=< doors=4 paint=green > " +
" Ford=< doors=2 paint=purple > " +
" Nissan=< doors=3 paint=red >> " +
" % ThisIsAComment " +
" % [Another Comment] " +
" % < Also a comment > " +
" < &bike " +
" < inch=26 &green > " +
" < inch=27 &purple >>> " +
" " );

java.lang.System.out.println
( room.getRoom( 0 ).getRoom( "Ford" ).get( "doors" ));

java.lang.System.out.println
( room.getRoom( 0 ).getType() );

java.lang.System.out.println
( room.getRoom( 1 ).hasType( "bike" ));

for( final java.lang.Object o : room.getRoom( 1 ))
java.lang.System.out.println( o ); }}

2
car
true
< &green inch =26 >
< &purple inch =27 >

To read from a file »source.uno« instead, one would use:

de.dclj.ram.notation.unotal.RoomFromModule.roomFrom
( new java.io.File( "source.uno" ));

This notation is called »Unotal«.

http://www.purl.org/stefan_ram/pub/unotal_en

The Java implementation can read and write Unotal and
is available under the GPL as a part of ram.jar.

http://www.purl.org/stefan_ram/pub/ram-jar

(Drawbacks/Disclaimer: I am the only one using this library
right now and it is experimental (not very stable) yet.
(While the jar-file-download was found broken recently, the
zip file should work. I am still working on the project.))
 
S

Stefan Ram

Sanjay said:
I am wondering whether anybody know a way to load a properties
file with comments.

So comments are stripped on reading property files
(and also in the case of my custom format Unotal, by the way).

You might try to disguise commments as entries.

For example:

a = data
a.comment = a comment about a
b = data
b.comment = a comment about b
 
S

Sanjay

Stefan said:
So comments are stripped on reading property files
(and also in the case of my custom format Unotal, by the way).

You might try to disguise commments as entries.

For example:

a = data
a.comment = a comment about a
b = data
b.comment = a comment about b

Thanks Stefan. Looks like I will have to write my own Properties class.
I also need to keep order of keys the same. So extending properties
class won't work as it extends HashTable.
 
D

Daniel Pitts

I am wondering whether anybody know a way to load a properties file with
comments.

My problem is that I need to read a sample configuration file (same
format as java properties file) which has many sections in it and each
section has section name and some comments. This properties file is
bundled into a jar file. Then depending on some user input, I will
change some of the properties and then write the file to some other
location. However, I need to keep the format the same, including the
section comments and line breaks before them.

java.util.Properties seems to be a good fit for this purpose to me as I
can use load() and store() methods with pretty much no other code. Is
there a better way to do this? Thanks.

Sanjay

Considering that the Properies class was designed for this, I don't
see a reason not to use it.
Alternatively, you can use an XML format, but why do that
when .properties are easy to use?
 
A

Adam Maass

Sanjay said:
I am wondering whether anybody know a way to load a properties file with
comments.

My problem is that I need to read a sample configuration file (same format
as java properties file) which has many sections in it and each section
has section name and some comments. This properties file is bundled into a
jar file. Then depending on some user input, I will change some of the
properties and then write the file to some other location. However, I need
to keep the format the same, including the section comments and line
breaks before them.

java.util.Properties seems to be a good fit for this purpose to me as I
can use load() and store() methods with pretty much no other code. Is
there a better way to do this? Thanks.

Sanjay

Properties.load() ignores comment lines. That is, an instance of Properties
has no knowledge of any comments that were present the properties file used
to load the instance. Additionally, Properties.store() may not (probably
will not) preserve order; properties are, in fact, a simple hashtable --
which do not preserve order of the keys.

So, you're stuck having to write the .properties file yourself. You may also
have to read it yourself.

-- Adam Maass
 
L

Lew

Sanjay said:
Thanks Stefan. Looks like I will have to write my own Properties class.
I also need to keep order of keys the same. So extending properties
class won't work as it extends HashTable.

You can always sort the properties.keySet().

What's wrong with using Properties files that have comments in them?
 
S

Sanjay

What's wrong with using Properties files that have comments in them?

nothing wrong with using properties files that have comments in them. In
fact, that is what I have and when I will write a modified file, I would
like to preserve the comments. Can't do that with Properties class as it
ignores comments while loading it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top