Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

load properties file with comments

Thread view: 
Sanjay - 11 Apr 2007 21:54 GMT
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
Stefan Ram - 11 Apr 2007 22:16 GMT
>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.))
Stefan Ram - 11 Apr 2007 22:26 GMT
>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
Sanjay - 11 Apr 2007 23:22 GMT
>> I am wondering whether anybody know a way to load a properties
>> file with comments.
[quoted text clipped - 10 lines]
> 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.
Lew - 12 Apr 2007 13:15 GMT
> 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?

Signature

Lew

Sanjay - 12 Apr 2007 13:21 GMT
> 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.
Daniel Pitts - 12 Apr 2007 00:36 GMT
> I am wondering whether anybody know a way to load a properties file with
> comments.
[quoted text clipped - 12 lines]
>
> 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?
Adam Maass - 12 Apr 2007 07:00 GMT
"Sanjay" <sanjay.debian@gmail.invalid.com> wrote :
>I am wondering whether anybody know a way to load a properties file with
>comments.
[quoted text clipped - 12 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.