java工程中,我们经常会把一些重要的参数,配置到config.properties文件中,那么就需要有一个简单的工具类去读取这些值,下面是一个简单的小实例
public static Properties getConfigFilePath(String filename) {
Properties props = new Properties();
InputStream infile = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
try {
props.load(infile);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
infile.close();
}catch(Exception ex){
}
}
return props;
}
说明:调用此方法的时候,需要传入对应的文件名config.properties,或者其他的.properties文件,文件的内容是 key=value ,只需要在下面的方法中用
p.getProperty(key)就可以在类中取出对应的值