Programing/Springboot

Java Properties load, store example 읽고 수정

리커니 2020. 8. 21.
반응형

Java Properties load, store example 읽고 수정

 

Spring boot 의 설정파일인 .properties 파일에 접근하여

내용을 읽고 수정하고 저장하는 방법을 알아보도록 하겠습니다.

 

샘플 코드를 보시죠.

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import kr.co.neighbor21.wuis.vo.ServerSettingVO;
import lombok.extern.slf4j.Slf4j;

/**
 * @Class Name : PropertiesConfig
 * @Description : 설정파일을 읽어 전역객체로 전환하는 config class 
 * @Modification Information
 * @ 
 * @  수정일     		  수정자               수정내용
 * @ ----------   ---------   -------------------------------                
 * @ 2020.08.20    geonlee    최초생성
 * 
 * @author geonlee
 * @version 1.0.0
 * @since 2020-08-20
 */

@Configuration
@Slf4j
public class PropertiesConfig {
	
	public static ServerSettingVO settings = new ServerSettingVO();
	
	@Bean
	void CheckFile() {
		File propertiesFile = new File("ws.properties");
		Properties p = new Properties();
		InputStream is = null;
		if(!propertiesFile.exists()) {
			try {
				OutputStream os = null;
				propertiesFile.createNewFile();
				is = new FileInputStream("ws.properties");
				p.load(is);
				p.setProperty("ws.gridTbl", "");
				p.setProperty("ws.logTbl", "");
				p.setProperty("ws.currTbl", "");
				p.setProperty("ws.apiKey", "");
				p.setProperty("ws.url", "http://testUrl");
				os = new FileOutputStream("ws.properties");
				p.store(os, null);
			} catch (IOException e) {
				log.error("Properties File을 생성하는데 실패하였습니다.", e);
			}
		}else {
			try {
				is = new FileInputStream("ws.properties");
				p.load(is);
				String gt = p.getProperty("ws.gridTbl");
				String lt = p.getProperty("ws.logTbl");
				String ct = p.getProperty("ws.currTbl");
				String apiKey = p.getProperty("ws.apiKey");
				settings.setGridTbl(gt);
				settings.setLogTbl(lt);
				settings.setCurrTbl(ct);
				settings.setApiKey(apiKey);
			} catch (IOException e) {
				log.error("Setting 파일 설정에 실패하였습니다.", e);
			}
		}
	}
}

 

ws.properties 라는 설정파일이 없을 경우 만들어서 프로젝트 경로에 생성하고,

있을 경우 해당 설정파일의 변수를 읽어 static 변수에 추가하는 클래스 입니다. 

 

Properties 파일은 java.util 에서 제공하는 Properties 객체를 활용하여 파일을 FileInputStream으로 읽고

load 매소드를 활용하여 해당 파일을 읽어드립니다. 

 

InputStream is = null;
is = new FileInputStream("ws.properties");
p.load(is);

 

properties 파일이 load 가 되면 getProperty 메소드를 통해서 해당 key의 value에 접근 할 수 있습니다. 

ex) Properties.getProperty("key");

/*ws.gridTbl key에 접근하여 value를 리턴*/
String gt = p.getProperty("ws.gridTbl");

 

key의 추가나 수정은 setProperty 메소드를 통해서 합니다. 

없을 경우 추가되며, 있을 경우 value 값이 수정이 됩니다.

ex) Properties.setProperty("key", "value");

/*key가 없을 경우 추가, 있을 경우 value 수정*/
p.setProperty("ws.gridTbl", "testTbl");

 

key - value 값을 영속적으로 변경을 하려면 store 메소드를 활용합니다.

ex) Properties.store(FileOutputStream, "Comment");

 

OutputStream os = null;
Properties p = new Properties();
os = new FileOutputStream("ws.properties");
p.store(os, null);

 

반응형

댓글

💲 추천 글