Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #4021

Re: Reading a properties file so that keys can be retrieved in order

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Reading a properties file so that keys can be retrieved in order
Date 2011-05-12 16:14 -0400
Organization albasani.net
Message-ID <iqhf3m$bj8$1@news.albasani.net> (permalink)
References <c12651b9-38b4-4604-9084-4bfd2aa0ba86@v31g2000vbs.googlegroups.com>

Show all headers | View raw


On 05/12/2011 03:53 PM, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm using Java 1.6.  Let's say I have a properties file ...
>
> a=1
> b=2
> c=3
> ...
>
> How can i read the properties file in Java such that when I retrieve
> the key set, the iterator structure would return the keys in the order
> they are found in the file (in the above example, "a", "b", "c")?

You'll need custom code.  The 'java.util.Properties' type inherits from 
'java.util.Hashtable' and there is no guarantee that the table will retain its 
order even after you've loaded the properties, let alone the order specified 
by the file, depending on its use.

You should explain why the order matters, because the need influences what 
sort of data structures and algorithm you'll use.

If the order is determined at compile time, you can use an enum to specify the 
property names and retrieve the values by iterating through the enum 
'values()' to get the keys, and using those keys to retrieve the properties.

  for ( Orderer ord : Orderer.values() )
  {
     doSomethingWith( properties.get( ord.toString() ));
  }

If the order is dynamic, you'll have to go through more effort to create a 
'List orderers'.


  List <String> orderers = loadOrderers();

  for ( String ord : orderers )
  {
     doSomethingWith( properties.get( ord ));
  }

Generally with properties or preferences, they should be "load once into a 
custom structure", not "load every time you need a property".  So load the 
properties into an ordered structure at program start or at classload time. 
Don't keep the 'Properties' itself around.  Then iterate through that custom 
structure when you need a property.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Reading a properties file so that keys can be retrieved in order "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2011-05-12 12:53 -0700
  Re: Reading a properties file so that keys can be retrieved in order Lew <noone@lewscanon.com> - 2011-05-12 16:14 -0400
    Re: Reading a properties file so that keys can be retrieved in order Lew <noone@lewscanon.com> - 2011-05-12 21:13 -0400

csiph-web