java web工程讀取及修改設定檔

來源:互聯網
上載者:User

標籤:

這篇部落格比自己講解的詳細:

http://blog.sina.com.cn/s/blog_69398ed9010191jg.html

使用方法:

1)設定檔在web-info的class目錄下,或者說,eclipse工程的src目錄下

2)問題:修改設定檔之後,若不重啟伺服器,設定檔能夠即刻即時生效嗎?自己利用tomcat測試的結果是可以,但最好進一步確認一下

package com.bobo.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.util.HashMap;import java.util.Properties;public class PropertiesUtil {    /**     * @param args     */    public static void main(String[] args) {        String fileName = "test.properties";        PropertiesUtil pu = new PropertiesUtil();        HashMap map=new HashMap();        map.put("age", "24");        map.put("name", "leishao");        pu.updateProperty(fileName, map);        System.out.println(pu.loadAllProperties(fileName));        System.out.println(pu.loadProperty(fileName, "name"));            }    public HashMap<String, String> loadAllProperties(String fileName) {        InputStream is = PropertiesUtil.class.getClassLoader()                .getResourceAsStream(fileName);        BufferedReader br = new BufferedReader(new InputStreamReader(is));        Properties pr = new Properties();        try {            pr.load(br);            HashMap<String, String> hm = new HashMap<String, String>();            for (Object s : pr.keySet()) {                hm.put(s + "", pr.getProperty(s + ""));            }            return hm;        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        return null;    }    public String loadProperty(String fileName, String key) {        InputStream is = PropertiesUtil.class.getClassLoader()                .getResourceAsStream(fileName);        BufferedReader br = new BufferedReader(new InputStreamReader(is));        Properties pr = new Properties();        try {            pr.load(br);                        if (pr.containsKey(key)) {                return pr.get(key) + "";            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        return null;    }    public void updateProperty(String fileName, HashMap<String,String> map) {        InputStream is = PropertiesUtil.class.getClassLoader()                .getResourceAsStream(fileName);        BufferedReader br = new BufferedReader(new InputStreamReader(is));        Properties pr = new Properties();        String filePath=PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();                             try {            pr.load(br);            pr.putAll(map);            OutputStream out=new FileOutputStream(filePath);            pr.save(out, "");                                } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (br != null) {                try {                    br.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }             }    }

 

java web工程讀取及修改設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.