• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem wiht the load() method while using the Properties object

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
Well, I�m having a kind of side effect while using the load() Properties Object method .Appart from loading correctly all the parameters and values, it changes its order , i.e., it�s scrambling the original order (in terms of lines only ) and it affects seriously the aim of the program...
In fact, although this method needs and InputStream object associated , I�m using on of its subclasses (FileInputStream) ... But...I don�t think this might be the problem...
In fact, I�ve already created an enumeration with propertyNames , which worked very well, but my problem is that these data are in a different order regarding the file loaded. There�s no trace of sorting or some �logical scrambling�... (and I can�t change the parameters order in my application ...)
It�s interesting to note that up to 5 elements , the original order of the elements is kept... After that, the �auto-scrambling� begins...
Ex :
Consider the ListOfParameters.txt file as the source for the properties object:
MainBoilerstatus = off
SpareBoiler = on
MainValve = open
SecondaryValve = open
SpareValve = off
Container1 = presunto
Container2 = chester
Container3 = peru
After having loaded this list in a Properties object , if I list it with the list() method , I�ll have something like this :
SecondaryValve = open
Container3 = peru
SpareBoiler = on
Container2 = chester
MainBoilerstatus = off
Container1 = presunto
MainValve = open
SpareValve = off
Why the original order is changed when it�s loaded into a Properties object ???
So , please, how can I avoid this �side effect� ?
[ February 05, 2002: Message edited by: fabio gama ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't really - not with Properties. It extends Hashmap, and when it executes list() the order is largely determined by the order of the hash values. I suppose you could extend Properties yourself to override this behavior - you could maintain a List in a private instance field which contains the actual property values, and use the value in the Hashtable to store the index. Whenever a value is added or accessed, you'd need to use both Hashtable and List in concert - but the List would retain the order in which things were added.
If you decide to do this though, it's probably best to just wrtie a new class which contians a List and a Map, rather than actually extending Properties - since Properties should never have been built directly on Hashtable anyway. You can mess up a Properties by accessing the Hashtable methods directly. Rather than explain this in the documentation, they should hve simply made it impossible by excapsulating the Hashtable rather than extending it. Oh well... You can copy some of the source code from Properties to use in a new OrderedProperties class - like the load() method - and use the same method names that it uses, for simplicity. Just modify them to (a) keep track of the order in a List, and (b) hide the Hashtable/Map inside, as another private field. Enjoy...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic