Java Read configuration file Properties__java

Source: Internet
Author: User

The

Properties class inherits from the Hashtable class and implements the map interface, and also holds the property set in the form of a key-value pair. But properties have a special place, that is, its keys and values are string types. The

code is as follows:

Package cn.zhangheng.filter;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;

Import java.util.Properties;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

Import Javax.servlet.http.HttpServletRequest;

Import Org.apache.log4j.Logger; /** * My filter * * @author Zhangheng * @date August 16, 2016 Morning 10:14:40 * * */public class Myfilter implements filter {p
    ublic static Logger LOG = Logger.getlogger (Myfilter.class);

    public static Properties P = new properties (); The public void Destroy () {///only executes log.info when the Web server is destroyed ("End filter ...").
        ");
    SYSTEM.OUT.PRINTLN ("-----Only one---"); public void Dofilter (ServletRequest request, servletresponse response, Filterchain Filterchain) throws
     IOException, Servletexception {   Log.info ("Perform filtering ...")
        ");
        String Servletcontextpath = Request.getservletcontext (). Getcontextpath ();
        System.out.println (Servletcontextpath);
        HttpServletRequest Request1 = (httpservletrequest) request;
        String URL = Request1.getrequesturi ();
        if (!url.contains ("login.do")) {Filterchain.dofilter (request, response); Web.xml (Non-javadoc) * * @see Javax.servlet.filter#init can be obtained when initialization is performed (Javax.servlet.Fi LTERCONFIG) */public void init (Filterconfig filterconfig) throws Servletexception {Log.info ("Initialize filter ...
        ");
        String filtername = Filterconfig.getfiltername ();
        String param1 = Filterconfig.getinitparameter ("Firstparamname");
        String param2 = Filterconfig.getinitparameter ("Secondparamname");
        System.out.println ("filtername =" + FilterName + ";p aram1 =" + param1 + ";p aram2 =" + param2); enumeration<string> s = filterconfig.getinitparameternames();
            while (S.hasmoreelements ()) {string string = (String) s.nextelement ();
        System.out.println (string);
        }//Get XML InputStream in = MyFilter.class.getResourceAsStream ("/para.properties"); try {p.load (in);//This method can be loaded from the file input stream corresponding to the. Properties property file, loading the property list to the Properties class object Log.info (P.getproperty ("NA

            Me ") + P.getproperty (" age ");//The two methods are to get and set property information respectively.
            Append FileOutputStream out = new FileOutputStream ("/b.properties", true);
            P.setproperty ("Phone1", "1232451"); P.store (out, append information);//This method saves the property list of the Properties class object to the output stream. If the comments is not empty, the first row of the Saved property file is #comments, which indicates the annotation information, and if it is empty there is no annotation information.
        The comment information is followed by the current save time information for the property file.
        catch (IOException e) {e.printstacktrace ();
        finally {P.clone ();
 }
    }

}

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.