Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Reading a properties file so that keys can be retrieved in order Date: Thu, 12 May 2011 16:14:52 -0400 Organization: albasani.net Lines: 52 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net zFR2Ejb2eiLbSjoxy76ttBa8IB9UpwYZucLAO8t8pEP2bBnZjn64jPYvsGWVW/Dyk3rJOi8Oyj8f7xs/AUDm0Ih8alOQ03NibPj1OQPVlfCoFpa1DJatG/jQTPhnWVm7 NNTP-Posting-Date: Thu, 12 May 2011 20:14:46 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="ipAS5I/9l0TysRPvSwXJFIfa52/3EzLf9KZjM+OZ20oV7GDylBDPcIUHo3mMVOUu9yoqUSA82so1yQ9n24UXcvnPYrhhY/5O+q9Pb/2arfWGj28ckaMWDwXqJ7c57QIm"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:q2q0pz9cYgSAQ4nB37KcKfdIofM= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4021 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 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