Results 1 to 10 of 10
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default property values into an Enumeration object in the original order

    Dear Readers,

    I have got a property file with the following content

    reptile = crocodile
    mammal = dog
    bird = albatross
    fish = carp


    And I would like collect the property values into an ArrayList. Reading the java documentation I found the keys() ( or the propertyNames() ) method, which returns an Enumeration object.

    Then I printed the content to the console. But I noticed that the program didn't display the values in the correct (the original) order.

    Here is the output result:

    carp
    dog
    albatross
    crocodile


    I would like the first line of the properties file will be the first item of the Enum. (the crocodile will be the first item of the enum)
    How can I achive it?
    Could you suggest a good, useful method or function instead of these "enum-type" methods?


    Thanks in forward best regards

    I enclosed my code below:

    Java Code:
    import java.util.Properties;
    import java.util.Enumeration;
    import java.io.IOException;
    import java.io.FileInputStream;
    
    public class props{
    	public props()
    	   {
    	    try{
    		Properties pr = new Properties();
    		FileInputStream fin = new FileInputStream("animal.properties");
    		pr.load(fin);
    	        Enumeration en = pr.keys();
    		while (en.hasMoreElements())
    			{
    			   String value = pr.getProperty((String)en.nextElement());
    			   System.out.println(value);
    			}
    
    	      }
    	     catch(IOException ioe)
    		 {
    		   ioe.printStackTrace();
    		 }
    	   }
    public static void main(String [] args)
    	{
    		new props();	  	
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I have got a property file with...
    But I noticed that the program didn't display the values in the correct (the original) order.
    They were not displayed in the same order as they appeared in the file. A Properties instance does not have an ordering (correct or incorrect); that is to say there is no ordering defined for the class. Consequently if you care about the order of a bunch of String/String associations then a properties file (read into an instances of Properties) is not the right sort of class to be using.

    What the correct class to be using is depends on what you are trying to do.

    One thing you could do is parse the file yourself (ie read line by line and extract the two strings) and put the results into a LinkedHashMap. The important thing about LinkedHashMap instances is that they do respect insertion order.

    Of course you get an ordered map this way, but you lose the Goodness of a Properties instance (default values, i/o methods). If using an ordered map, as above, won't meet your needs, perhaps you could say what they are.

    ------

    My internet connection went down a couple of hours ago when I wanted to post this ;(

    @Fubarable: yes, a map. But not a TreeMap as this will sort according to the keys in some way you have to specify in advance.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by pbrockway2 View Post
    @Fubarable: yes, a map. But not a TreeMap as this will sort according to the keys in some way you have to specify in advance.
    Oops, thanks for the correction. Don't know what I was smoking yesterday. :eek:

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Oops, thanks for the correction. Don't know what I was smoking yesterday. :eek:
    Maybe the OP can override the put(Object key, Object value) method in the Properties method while reading so the key/value pairs would come in in the order they are read. (*)

    kind regards,

    Jos

    (*) I consider this a dirty hack ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi guys, thanks for your helps and comments.
    I considered my property file as a text file and, I parsed it with the Scanner object. I cared about the format of Java-style property files. (I listen to comment lines, and separator marks)
    I print the key-value pairs with the Map.Entry object.

    here is my code snippet that shows how to parse the file

    Java Code:
    public propLoad2()
    	   {
    	    try{
    		Properties pr = new Properties();
    		FileInputStream fin = new FileInputStream("animal.properties");
    		Scanner scan = new Scanner(fin);
    
    		LinkedHashMap lmap = new LinkedHashMap();
    
    	        while (scan.hasNext())
    			{
    	  		   scan.useDelimiter("\n");
    			   String nextStr=(String)scan.next();
    			   if (!nextStr.startsWith("#")  && !nextStr.startsWith("!") )
    				{
    				  int pos;
    				  if ((pos=nextStr.indexOf("="))>0 || (pos=nextStr.indexOf(":"))>0 )
    					{
    					 				
    
    lmap.put(nextStr.substring(0,pos-1) , nextStr.substring(pos+1).trim()
    );
    					}
    				  else 
    					{
    					   lmap.put(nextStr,"");
    					}
    					
    
    				}
    			}
    		 Set set = lmap.entrySet();
    		 Iterator it = set.iterator();
    		 while (it.hasNext())
    			{
    			   Object itStr=it.next();
    			   Map.Entry entry = (Map.Entry)itStr;	
    			   System.out.println("Key:" + entry.getKey());
    			   System.out.println("Value: " + entry.getValue());				
    			}
    
    	       }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Now you're not using the variable Properties pr anymore; it can be removed.

    kind regards,

    Jos

    ps. also see my reply #5 so you don't have to do all that reading yourself ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Thanks your quick advise, I removed the unnecessary object.

    And now I scratching my head about your dirty hack (#5) :)
    it would be a good idea...

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And now I scratching my head about your dirty hack (#5)

    It's discussed with interesting comment on StackOverflow.

  10. #10
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi,
    thanks your reference, it was even interesting. I could never find the solution from my own mind:)
    I enclosed my code and I used "System.out.println" command for illustration purpose.
    As I can see the output, the Properties.load() function uses the HashTable's put() method. Then we overrode this method. And we overrode the Enumeriton's keys() method also and I guess this method sorts the items. (to be honest the function of the keys() method is not yet clear fully to me at least - but I often debug:D )

    Thanks again.

    here is my code:

    [CODE]
    import java.util.Properties;
    import java.util.HashMap;
    import java.util.Enumeration;
    import java.util.LinkedHashSet;
    import java.util.Collections;

    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;

    public class Prop extends Properties{

    private LinkedHashSet Key = new LinkedHashSet();
    public Prop ()
    {
    try{
    System.out.println("load starts");
    load(new DataInputStream(new FileInputStream("fruit.properties")));
    System.out.println("property file loaded...");
    System.out.println("keys without ordering: "+keySet());
    System.out.println("enum definition starts...");
    Enumeration em = keys();
    System.out.println("enumeration object defined...");

    while (em.hasMoreElements())
    {
    String str = getProperty((String)em.nextElement());
    System.out.println(str);
    }
    }

    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    }

    public Enumeration keys()
    {
    System.out.println("override enumeration - keys() method");
    return Collections.enumeration(Key);
    }

    public Object put(Object key,Object value)
    {
    Key.add(key);
    System.out.println("overrides Put method");
    return super.put(key,value);
    }

    public static void main(String[] args)
    {
    new Prop();
    }
    }
    [\CODE]

Similar Threads

  1. Comparing values within an Object
    By Bake1tEazy in forum New To Java
    Replies: 10
    Last Post: 02-09-2011, 02:05 PM
  2. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 07:59 AM
  3. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM
  4. Replies: 0
    Last Post: 11-21-2008, 08:17 AM
  5. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •