Java 讀取設定檔Properties__Java

來源:互聯網
上載者:User

 Properties類繼承自Hashtable類並且實現了Map介面,也是使用一種索引值對的形式來儲存屬性集。不過Properties有特殊的地方,就是它的鍵和值都是字串類型。
 
代碼如下:

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;/** * 我的過濾器 *  * @author zhangheng * @date 2016年8月16日上午10:14:40 * */public class MyFilter implements Filter {    public static Logger LOG = Logger.getLogger(MyFilter.class);    public static Properties p = new Properties();    public void destroy() {        // 只有在web伺服器銷毀的時候才會執行        LOG.info("結束過濾。。。");        System.out.println("-----只執行一次---");    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)            throws IOException, ServletException {        LOG.info("執行過濾。。。");        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(javax.servlet.FilterConfig)     */    public void init(FilterConfig filterConfig) throws ServletException {        LOG.info("初始化過濾。。。");        String filterName = filterConfig.getFilterName();        String param1 = filterConfig.getInitParameter("firstParamName");        String param2 = filterConfig.getInitParameter("secondParamName");        System.out.println("filterName = " + filterName + ";param1 = " + param1 + ";param2 = " + param2);        Enumeration<String> s = filterConfig.getInitParameterNames();        while (s.hasMoreElements()) {            String string = (String) s.nextElement();            System.out.println(string);        }        // 擷取xml        InputStream in = MyFilter.class.getResourceAsStream("/para.properties");        try {            p.load(in);//這個方法可以從.properties屬性檔案對應的檔案輸入資料流中,載入屬性列表到Properties類對象            LOG.info(p.getProperty("name") + p.getProperty("age"));//這兩個方法是分別是擷取和設定屬性資訊。            // 追加            FileOutputStream out = new FileOutputStream("/b.properties", true);            p.setProperty("phone1", "1232451");            p.store(out, "追加資訊");//這個方法將Properties類對象的屬性列表儲存到輸出資料流中。如果comments不為空白,儲存後的屬性檔案第一行會是#comments,表示注釋資訊;如果為空白則沒有注釋資訊。注釋資訊後面是屬性檔案的當前儲存時間資訊。        } catch (IOException e) {            e.printStackTrace();        } finally {            p.clone();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.