[JAVA] 프로퍼티 파일 읽기 직접 구현 (PropertiesUtil 클래스의 readPropertiesFile 메서드)
자바에서 정석적인 프로퍼티 파일 읽기는 아래 포스트를 참고하면 된다.
https://blog.naver.com/bb_/221666064467
위 링크는 정석적인 프로퍼티 파일 읽기이고, 본 포스트는 프로퍼티 파일 읽기를 직접 구현해본 것이다.
|
// 프로퍼티 파일 읽기 (PropertiesUtil 클래스의 readPropertiesFile 메서드)
// UTF-8 인코딩 형식의 properties 파일을 읽어서 HashMap 객체로 만들어 리턴하는 메서드.
package com.bb.classmerge.util;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap;
public class PropertiesUtil {
/** * UTF-8 인코딩 형식의 properties 파일을 읽어서 HashMap 객체로 만들어 리턴한다. * * @param propFilePath * @return * @throws Exception */ public static HashMap<String, String> readPropertiesFile(String propFilePath) throws Exception { if (propFilePath == null || propFilePath.length() == 0) { throw new Exception(“PropertiesUtil readPropertiesFile : propFilePath == null || propFilePath.length() == 0”); }
File propFileObj = new File(propFilePath); if (!propFileObj.exists()) { throw new Exception(“PropertiesUtil readPropertiesFile : propFileObj does not exists. [“ + propFileObj.getAbsolutePath() + “]”); } if (!propFileObj.canRead()) { throw new Exception(“PropertiesUtil readPropertiesFile : propFileObj can not read. [“ + propFileObj.getAbsolutePath() + “]”); }
HashMap<String, String> resultMap = new HashMap<String, String>();
FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null;
try { fileInputStream = new FileInputStream(propFileObj); inputStreamReader = new InputStreamReader(fileInputStream, “UTF-8”); bufferedReader = new BufferedReader(inputStreamReader);
String oneLine = null; while ((oneLine = bufferedReader.readLine()) != null) { if (oneLine == null || oneLine.length() == 0) { continue; } // 주석 무시 if (oneLine.trim().startsWith(“#”)) { continue; } int equalIndex = oneLine.indexOf(“=”); if (equalIndex < 0) { continue; } // 좌측값(key값)만 trim 처리한다. 우측값(value값)은 의도적으로 공백이 포함될 수 있다고 판단한다. String leftText = oneLine.substring(0, equalIndex).trim(); String rightText = oneLine.substring(equalIndex + 1); // 등호 좌측 텍스트가 존재하지 않을 경우 무시 if (leftText.length() == 0) { continue; } resultMap.put(leftText, rightText); }
} catch (IOException e) { throw e;
} catch (Exception e) { throw e;
} finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (Exception e) { // 무시 } finally { bufferedReader = null; }
try { if (inputStreamReader != null) { inputStreamReader.close(); } } catch (Exception e) { // 무시 } finally { inputStreamReader = null; }
try { if (fileInputStream != null) { fileInputStream.close(); } } catch (Exception e) { // 무시 } finally { fileInputStream = null; } }
return resultMap; } }
|