java read config from file
public final class Config {
public static String ES_HOST_STRING;
public static String ES_PORT_STRING;
static{
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
try {
prop.load(inputStream);
ES_HOST_STRING = prop.getProperty("loganalysis.es.host");
ES_PORT_STRING = prop.getProperty("loganalysis.es.port");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
try {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。