Objective
A few days ago, to do a JSP read and write properties of the module, intended to enable users to edit the system configuration in the Web interface. I naturally use the Java Porperties Tool class, read the write does not know how convenient, but the user said to modify a save after the next time you can not find their own things (order chaos). therefore need to do an ordered properties file read-write class. Decisively on the StackOverflow, found two examples (links do not remember), two ways, copied down.
Way One, linkedproperties
Package Com.example.common;import Java.io.ioexception;import Java.io.writer;import java.util.arraylist;import Java.util.collections;import Java.util.enumeration;import Java.util.linkedhashmap;import Java.util.List;import Java.util.map;import Java.util.properties;import Java.util.set;public class Linkedproperties extends Properties {priv Ate static final long serialversionuid = 1L; Private Map<object, object> linkmap = new linkedhashmap<object,object> (); @Override public Synchronized enumeration keys () {list<object> keys = new arraylist<object> (); For (Map.entry<object, object> entry:linkMap.entrySet ()) {Keys.add (Entry.getkey ()); } return Collections.enumeration (keys); } @Override public synchronized object put (object key, Object value) {Super.put (key, value); Return Linkmap.put (key, value); } @Override Public Synchronized Boolean contains (Object value) {return LinKmap.containsvalue (value); } @Override public Boolean containsvalue (Object value) {return linkmap.containsvalue (value); } @Override public synchronized enumeration<object> elements () {throw new unsupportedoperationexception ("Enumerations is so old-school, Don's use them," + "use KeySet () or EntrySet () instead"); } @Override public Set<map.entry<object, object>> EntrySet () {return linkmap.entryset (); } @Override public synchronized void Clear () {super.clear (); Linkmap.clear (); } @Override Public Synchronized Boolean containskey (Object key) {return Linkmap.containskey (key); } public void Store (writer writer, String comments) throws IOException {Super.putall (LINKMAP); Super.store (writer, comments); }}
Mode two, sequencedproperties
Package Com.example.common;import Java.util.arraylist;import Java.util.collections;import java.util.Enumeration; Import Java.util.list;import java.util.map;import java.util.Properties; @SuppressWarnings ({"Rawtypes", "Unchecked"} public class Sequencedproperties extends Properties {private static final long serialversionuid = 1L; Private List keylist = new ArrayList (); @Override public Synchronized enumeration keys () {return collections.enumeration (keylist); } @Override public synchronized object put (object key, object value) {if (!containskey (key)) {Ke Ylist.add (key); } return Super.put (key, value); } @Override public Synchronized object Remove (object key) {Keylist.remove (key); return Super.remove (key); } @Override public synchronized void Putall (Map values) {for (Object Key:values.keySet ()) {if (! ContainsKey (key)) {Keylist.add (key); } } Super.putall (values); }}
Read and write the properties file in an orderly manner--overwrite the java.util.Properties