728x90
300x250

[Sun Sys] 자바(Java) - Properties 파일 읽기


자바를 통해서 Properties 파일을 읽는 방법에 대해서 정리하였다.



1. properties 파일


폴더: /src/main/resources


database=localhost

dbuser=helloUser

dbpassword=password


* 파일명: configuration.properties



2. 자바 - 코드


public class GetPropertyValue {


      private String dbpassword;

private String database;

private String dbuser;


      InputStream inputStream;


      public String getPropertyValue() throws Exception{

try{

Properties prop = new Properties();

String propFileName = "configuration.properties";

inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

if (inputStream != null) {

prop.load(inputStream);

} else {

throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");

}


dbpassword = prop.getProperty("dbpassword");

database = prop.getProperty("database");

dbuser = prop.getProperty("dbuser");

 

} catch(Exception e){

logger.info("Exception : " + e);

} finally {

inputStream.close();

}

return result;

}


}


* 파일명: GetPropertyValue.java

반응형

+ Recent posts