본문 바로가기
Programming/JAVA

[JAVA] 외부파일 값 변경하기

by Pendine 2022. 7. 27.
728x90
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public abstract class ConfigHandler {

	private static String basicFilePath = System.getProperty("user.dir") + File.separator +"conf"+File.separator + "connected.properties";
	
	private static File propertiesFile = new File( basicFilePath );

	public static void setConfigParameter(String paramName, String settingValue) {
		setConfigParameter(paramName, settingValue, basicFilePath); 
	}
	
	public static void setConfigParameter(String paramName, String settingValue, String filePath) {
		String path = filePath;
		
		File propertyFile = new File(path);
		
		if(!propertyFile.exists()) {
			path = basicFilePath;
			propertyFile = new File(path);
			if(!propertyFile.exists()) {
				new Exception("property File Do not exist File input path : " + filePath + " / base path : " + basicFilePath);
			}
		}
		
		if(propertyFile.exists()){
			try {
				
				Properties p = new Properties();
				
				InputStream is = null;
				
				OutputStream os = null;
				
				is = new FileInputStream( filePath );

				p.load(is);
				
				System.out.println("paramName : " + paramName + "/ value : " + p.getProperty(paramName) );
				
				p.setProperty(paramName, settingValue);
				
				System.out.println("settingValue : " + settingValue );

				os = new FileOutputStream(path);
//				p.store(os, path);
				p.store(os, null);

				System.out.println("paramName : " + paramName + "/ value : " + p.getProperty(paramName) );
				
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static String getConfigParameter(String paramName, String filePath) {
		String path = filePath;
		
		File propertyFile = new File(path);
		
		if(!propertyFile.exists()) {
			path = basicFilePath;
			propertyFile = new File(path);
			if(!propertyFile.exists()) {
				new Exception("property File Do not exist File input path : " + filePath + " / base path : " + basicFilePath);
			}
		}
		
		if(propertyFile.exists()){
			try {
				
				Properties p = new Properties();
				
				InputStream is = null;
				
				is = new FileInputStream( filePath );

				p.load(is);
				
				return p.getProperty(paramName);
				
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	public static String getConfigParameter(String paramName) {
		return getConfigParameter(paramName , basicFilePath);
	}

	public static void addConfigParameter(String paramName, String settingValue, String filePath) {
		String targetParam = getConfigParameter( paramName , filePath);
		int targetParamInt = -9999;
		int settingParamInt = -9999;
		try {
			targetParamInt = Integer.parseInt(targetParam);
		}catch(Exception e) {
			System.out.println("parsing(String -> int) Error Parameter name : " + paramName + " / filePath : " + filePath);
			e.printStackTrace();
		}
		
		try {
			settingParamInt = Integer.parseInt(settingValue);
		}catch(Exception e) {
			System.out.println("parsing(String -> int) Error setting Parameter name : " + settingValue + " / filePath : " + filePath);
			e.printStackTrace();
		}
		
		
		String finalSettingValue = null;
		
		if(targetParamInt != -9999 && settingParamInt != -9999) {
			targetParamInt = targetParamInt + settingParamInt;
			finalSettingValue = String.valueOf(targetParamInt);
		}
		
		System.out.println("settingValue : " + finalSettingValue);
		
		if(finalSettingValue != null) {
			setConfigParameter(paramName, finalSettingValue, filePath);
		}
	}
	
	public static void addConfigParameter(String paramName, String settingValue) {
		addConfigParameter(paramName, settingValue, basicFilePath);
	}
	
}

특정 경로에 있는 파일을 읽고, 컨피그의 변수명의 값을 String형으로 불러옴.

 

add~로 시작하는 메서드 명은 파라미터 변수명(문자열), 세팅값(문자열)을 인자값으로 받고

파라미터 변수명의 값을 세팅값과 더해서

외부 파일인 config 파일의 값을 수정함.

728x90

댓글