Order
a few days ago, there was a requirement in the company project to read the configuration file, and it was best to ensure that the order loaded into memory was consistent with the order in the configuration file, but that if the Properties class provided in the JDK was used, the order of loading into memory was random after the configuration file was read, There is no guarantee that the order of the original files is consistent, so the Properties provided by the JDK do not work.
background
because of this need, and Java's Properties class can not be achieved, so can only think of another way. I used to think that when I loaded the configuration file into memory, I ordered it, but there were a lot of limitations and problems with this scenario. The information in the configuration file will be many, if you re-order it, the first will affect the performance of the system, secondly, the implementation of the program efficiency, there will be some impact. Finally, after some verification, colleagues found a similar article.
Solution Solutions
It is learned from the article that Java's properties loading property files is not guaranteed to match the order of the output, because properties are inherited from Hashtable, Key/value are directly present in Hashtable, and have Htable are not guaranteed to enter or exit the order.
The article has already provided the code, the idea is inherits from the Properties, overwrites the original put/keys,keyset,stringpropertynames can, in which uses a linkedhashset to save its all key. The complete code is as follows:
<span style= "Font-family:comic Sans MS;" >package Com.mstar.investmeplugin; Import Java.util.collections;import Java.util.enumeration;import Java.util.linkedhashset;import Java.util.properties;import Java.util.Set; /** * orderedproperties * @author LIII * @date 2015-07-10 */public class Orderedproperties extends Properties {privat E static final long serialversionuid = -4627607243846121965l; Private final linkedhashset<object> keys = new linkedhashset<object> (); Public enumeration<object> keys () {return collections.<object> enumeration (keys); } public object put (object key, Object value) {Keys.add (key); Return Super.put (key, value); } public set<object> KeySet () {return keys; } public set<string> Stringpropertynames () {set<string> Set = new linkedhashset<string> (); for (Object Key:this.keys) {set.add (String) key); } return Set; }}</span>
Calling Methods
<span style= "Font-family:comic Sans MS;" >properties props = new Orderedproperties ();p rops.load (FileInputStream); Traversing with keys (), KeySet () or stringpropertynames guarantees output in the order of the Files </span>
Concluding remarks
This particular demand, before it is not how to contact, not to pass this experience, found a little, their accumulation or very little, not much to say, continue to work hard.
Reference:
- StackOverflow
- Another implementation in the PLAYFRAMEWORK1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Read configuration files can be kept in sequence by Java Properties class