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


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

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
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 Thu, 12 May 2011 21:13:16 -0400
Organization albasani.net
Lines 168
Message-ID <iqi0j5$91r$1@news.albasani.net> (permalink)
References <c12651b9-38b4-4604-9084-4bfd2aa0ba86@v31g2000vbs.googlegroups.com> <iqhf3m$bj8$1@news.albasani.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net FgRbVpe9w+1/I9G7kLrSrxipI+HSFU9dgXbD3A4NBfkezxScaZsYUK6MQvDP+CWQ/EzKloyRhp3gHZJc8H03rA==
NNTP-Posting-Date Fri, 13 May 2011 01:13:09 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="nbDb4ueNUSc0ubeJTuDsDmEZNss+klS8yrM+aUvCUN62bUQ0RkhBxLe6kosZuAoLYc6qjXeOYZr3LzL5KfLbS+EDTMW2b4JoS2MQyvUXXq8ddTH5+myEk6DbIC4dkpjs"; 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 <iqhf3m$bj8$1@news.albasani.net>
Cancel-Lock sha1:RhYrOsH6He2Cu14xuFhhRRmCpz8=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4031

Show key headers only | View raw


laredotornado@zipmail.com wrote:
>> I'm using Java 1.6. Let's say I have a properties file ...
>>
>> a=1
>> b=2
>> c=3
>> ...
>>
>> How can i [sic] 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")?

<sscce source="com/lewscanon/eegee/Properteer.java">
/* Properteer.java
  * $Id$
  */
package com.lewscanon.eegee;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import java.util.Properties;

/**
  * Properteer.
  */
public class Properteer
{
     private static final String PROPFILE = "properteer.properties";

     /**
      * main.
      * @param args String [] command arguments.
      */
     public static void main( String [] args )
     {
         Reader propReader = new InputStreamReader(
                 Properteer.class.getResourceAsStream( PROPFILE ));

         assert propReader != null;

         Properties props = new Properties();
         try
         {
             props.load( propReader );
         }
         catch ( IOException exc )
         {
             final String msg  = "Unable to load properties: "
                 + exc.getLocalizedMessage();
             System.err.println( msg );
             IllegalStateException rethrow = new IllegalStateException( msg, 
exc );
             throw rethrow;
         }
         finally
         {
             close( propReader );
         }

         System.out.println( "size "+ props.size() +": " );
         System.out.println( "------" );
         System.out.println( "native order" );
         for ( Map.Entry <?, ?> entry : props.entrySet() )
         {
             System.out.println( entry.getValue().toString() );
         }
         System.out.println( "------" );

         System.out.println( "forced order" );
         for ( Orderer ord : Orderer.values() )
         {
             System.out.println( props.get( ord.toString() ));
         }
         System.out.println( "------" );
         System.out.println();

     }

     static void close( Closeable closeable )
     {
         assert closeable != null; // package-private - cannot accept null
         try
         {
             closeable.close();
         }
         catch ( IOException exc )
         {
             final String msg  = "Unable: "+ exc.getLocalizedMessage();
             System.err.println( msg );
        }
     }

     enum Orderer
     {
         FOO("foo"), BAR("bar"), BAZ("baz"), QUX("qux"), ;

         private final String handle;

         Orderer( String han )
         {
             this.handle = han;
         }

         @Override
         public final String toString()
         {
             return this.handle;
         }

         /**
          * fromString - convert from {@code String} to {@code Orderer}.
          * Tries custom strings first, then defaults to {@code valueOf()}.
          * @param han String to convert.
          * @return Orderer matching the input, or {@code null} if no match.
          */
         public static Orderer fromString( String han )
         {
             if ( han == null )
             {
                 return null;
             }
             for ( Orderer inst : values() )
             {
                 if ( han.equals( inst.toString() ) )
                 {
                     return inst;
                 }
             }
             return valueOf( han );
         }
     }

}
</sscce>

properteer.properties (in same subdirectory as Properteer.class):
----------------------
foo = The Fonz
bar = Richie Cunningham
baz = Joanie Cunningham
qux = Chachie
----------------------

output:
----------------------
size 4:
------
native order
Chachie
Richie Cunningham
Joanie Cunningham
The Fonz
------
forced order
The Fonz
Richie Cunningham
Joanie Cunningham
Chachie
------
----------------------

-- 
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 | 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