java web 讀取配置文件

若配置文件在classPath下用如下方法便可讀取配置文件,code:
java

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");
		}
	}
相關文章
相關標籤/搜索