[JAVA] SimpleProperties 클래스
자바에서 정석적인 프로퍼티 파일 읽기는 아래 포스트를 참고하면 된다.
https://blog.naver.com/bb_/221666064467
위 링크는 정석적인 프로퍼티 파일 읽기이고, 본 포스트는 프로퍼티 파일 읽기를 직접 구현해본 것이다.
<SimpleProperties 클래스>
properties 파일 읽기용 자바 클래스.
주요 기능은 properties 파일을 읽어서 HashMap으로 만드는 것이다.
클래스 하나만 갖다 놓으면 properties를 읽을 수 있게 간단한 형태로 만들어보았다.
단, SimpleLogger.printLog 라는 부분은 오류가 날 것이다.
System.out.println 코드로 대체해서 사용하거나, 다음 글에서 설명할 SimpleLogger 클래스를 갖다쓰면 된다.
cf) [JAVA] SimpleLogger 클래스 : https://blog.naver.com/bb_/221656929478
———-
package com.bb.simpleset;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class SimpleProperties {
/**
* 프로퍼티 파일 읽어서 해시맵으로 리턴한다.
*
* @param filePath
* @return
* @throws IOException
* @throws Exception
*/
public static HashMap<String, String> readPropertiesFile(String filePath) throws IOException, Exception {
if (filePath == null || filePath.length() == 0) {
SimpleLogger.printLog(“properties filePath is null or empty.”);
return null;
}
File file = new File(filePath);
if (!file.exists()) {
SimpleLogger.printLog(“properties file does not exists. [” + file.getAbsolutePath() + “]”);
return null;
}
HashMap<String, String> resultMap = new HashMap<String, String>();
try {
ArrayList<String> propFileContent = readFile(file);
if (propFileContent == null || propFileContent.size() == 0) {
SimpleLogger.printLog(“properties file content is empty. [” + file.getAbsolutePath() + “]”);
}
String line = null;
int propFileLineCount = propFileContent.size();
for (int i=0; i<propFileLineCount; i++) {
line = propFileContent.get(i);
if (line == null || line.trim().length() == 0) {
continue;
} else {
line = line.trim();
}
// #으로 시작하면 주석이므로 무시
if (line.startsWith(“#”)) {
continue;
}
int equalIndex = line.indexOf(“=”);
if (equalIndex < 0) {
continue;
}
String leftKey = line.substring(0, equalIndex);
if (leftKey.trim().length() == 0) {
continue;
} else {
leftKey = leftKey.trim();
}
String rightVlaue = line.substring(equalIndex + 1);
if (rightVlaue.trim().length() == 0) {
continue;
} else {
rightVlaue = rightVlaue.trim();
}
resultMap.put(leftKey, rightVlaue);
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
return resultMap;
}
/**
* 파일을 읽는다.
*
* @param file
* @return
* @throws IOException
* @throws Exception
*/
private synchronized static ArrayList<String> readFile(File file) throws IOException, Exception {
if (file == null || !file.exists()) {
return null;
}
ArrayList<String> resultList = null;
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream, “UTF-8”);
bufferedReader = new BufferedReader(inputStreamReader);
String oneLine = null;
while ((oneLine = bufferedReader.readLine()) != null) {
if (resultList == null) {
resultList = new ArrayList<String>();
}
resultList.add(oneLine);
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
close(bufferedReader);
close(inputStreamReader);
close(fileInputStream);
}
return resultList;
}
private static void close(FileInputStream fileInputStream) {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (Exception e) {
// 무시
} finally {
fileInputStream = null;
}
}
private static void close(InputStreamReader inputStreamReader) {
try {
if (inputStreamReader != null) {
inputStreamReader.close();
}
} catch (Exception e) {
// 무시
} finally {
inputStreamReader = null;
}
}
private static void close(BufferedReader bufferedReader) {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
// 무시
} finally {
bufferedReader = null;
}
}
}