웹 개발

[java] read properties file

노루아부지 2019. 7. 26. 09:34

1. 클래스 선언

public class Config() {
	private Properties configFile;
    
	public Config() {
		configFile = new java.util.Properties();

		try {
			// 방법 1 시작 (이 방법은 프로젝트 내부의 파일만 사용할 수 있다.)
			configFile.load(this.getClass().getClassLoader().getResourceAsStream("config.conf"));
			// 방법 1 끝

			// 방법 2 시작
			InputStream in = new FileInputStream("config.conf");
			configFile.load(in);
			in.close();
			// 방법 2 끝
		}
        catch(Exception e) {
			e.printStackTrace();
		}
	}

	public String getProperty(String key) {
		String value = this.configFile.getProperty(key);
		return value;
	}
}

 

2. 클래스 사용

cfg = new Config();
dbname = cfg.getProperty("dbname");

 

3. config file

# this is comment
dbname = mydbname
dbhost = mydbhost
dbpw = mydbpassword
728x90

'웹 개발' 카테고리의 다른 글

jquery plugin  (0) 2019.07.27
java.net.SocketException: Permission denied  (0) 2019.07.27
[java] BigInteger  (0) 2019.07.25
IE 8, 9 전용 css 핵  (0) 2019.07.24
[javascript] json array에서 원하는 field에 url encode, decode  (0) 2019.07.24
loading