项目中读取properties文件中配置信息方法

properties文件放在src根目录下。代码如下:

package com.web.util;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class RestartEffected {

    private Properties properties;

    public RestartEffected() {
        InputStream is = RestartEffected.class.getClassLoader().getResourceAsStream("restartEffected.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        properties = new Properties();
        try {
            properties.load(br);
            is.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key) throws IOException {
        return  properties.getProperty(key);
    }

//    public Map<String,String> getAllProperties() throws FileNotFoundException,IOException  {
//        //保存所有的键值
//        Map<String,String> map=new HashMap<String,String>();
//        Enumeration en = properties.propertyNames();
//        while (en.hasMoreElements()) {
//            String key = (String) en.nextElement();
//            String Property = properties.getProperty(key);
//            map.put(key, Property);
//        }
//        return map;
//    }

}
发布了49 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/shenju2011/article/details/95594724