Read and write the properties file in an orderly manner--rewriting the Java.util.Properties

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.