java 读取properties

来源:互联网 发布:删除windows.old 编辑:程序博客网 时间:2024/05/12 02:06
import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class ConfigUtils {    private static Properties configfile = null;    static {        InputStream in = ConfigUtils.class.getClassLoader()                .getResourceAsStream("config.properties");        configfile = new Properties();        try {            configfile.load(in);            in.close();        } catch (IOException e) {            System.out.println("No config.properties defined error");        }finally {            try {                if (in != null) {                    in.close();                }            } catch (IOException e) {                in = null;            }        }    }    public String getPropertiesByKey(String _key) {        return configfile.getProperty(_key);    }}

调用:

//spring mvc注解@Resourceprivate ConfigUtils config;@Overridepublic void exportExcel() {config.getPropertiesByKey(“key”)}
0 0