load properties file with comments

Discussion in 'Java' started by Sanjay, Apr 11, 2007.

  1. Sanjay

    Sanjay Guest

    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
     
    Sanjay, Apr 11, 2007
    #1
    1. Advertising

  2. Sanjay

    Stefan Ram Guest

    Sanjay <> writes:
    >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, Apr 11, 2007
    #2
    1. Advertising

  3. Sanjay

    Stefan Ram Guest

    Sanjay <> writes:
    >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
     
    Stefan Ram, Apr 11, 2007
    #3
  4. Sanjay

    Sanjay Guest

    Stefan Ram wrote:
    > Sanjay <> writes:
    >> 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
    >


    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.
     
    Sanjay, Apr 11, 2007
    #4
  5. Sanjay

    Daniel Pitts Guest

    On Apr 11, 1:54 pm, Sanjay <> wrote:
    > 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?
     
    Daniel Pitts, Apr 12, 2007
    #5
  6. Sanjay

    Adam Maass Guest

    "Sanjay" <> wrote :
    >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
     
    Adam Maass, Apr 12, 2007
    #6
  7. Sanjay

    Lew Guest

    Sanjay wrote:
    > 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?

    --
    Lew
     
    Lew, Apr 12, 2007
    #7
  8. Sanjay

    Sanjay Guest


    >
    > 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.
     
    Sanjay, Apr 12, 2007
    #8
  9. Sanjay

    arunudayan

    Joined:
    May 18, 2012
    Messages:
    1
    arunudayan, May 18, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    1
    Views:
    14,051
    Andrew Thompson
    Feb 25, 2004
  2. Replies:
    0
    Views:
    1,184
  3. RigasMinho

    Load Java Properties File using JSP

    RigasMinho, Sep 1, 2006, in forum: Java
    Replies:
    9
    Views:
    36,721
    RigasMinho
    Sep 5, 2006
  4. Monk
    Replies:
    10
    Views:
    1,544
    Michael Wojcik
    Apr 20, 2005
  5. Replies:
    4
    Views:
    656
    Dr John Stockton
    Jun 3, 2006
Loading...

Share This Page