java web工程讀取及修改配置文件


這篇博客比自己講解的詳細:

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();

                }
            }

        }
         
    }

    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM