若配置文件在classPath下用以下方法即可读取配置文件,code:
static{
Properties properties = new Properties();
Thread currentThread = Thread.currentThread();
ClassLoader contextClassLoader = currentThread.getContextClassLoader();
InputStream propertiesStream = contextClassLoader.getResourceAsStream("server.properties");
if (propertiesStream != null) {
try {
properties.load(propertiesStream);
//System.out.println(properties.getProperty("HOST"));
URL=properties.getProperty("HOST");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO close the stream
} else {
// Properties file not found!
System.out.println("not found");
}
}