一、
/*
* @(#)RWProperties.java 2010-10-28
*
* Copyright 2010 BianJing,All rights reserved.
*/
package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 用于读写Property属性配置文件的类
*
* @author BianJing<br/>
* E-mail:vipbooks@163.com
* @version 0.9 2010-10-28
*/
public class RWProperties {
private Properties properties;
/** 配置文件的完整路径 */
private String filePath;
/**
* 初始化RWProperties
*
* @param propertyFilePath
* 属性配置文件的完整路径,
* 如:"com/pagination/config/PaginationConfig.properties"
* @return RWProperties的实例
*/
public RWProperties(String propertyFilePath) {
filePath = propertyFilePath;
properties = getProperties(propertyFilePath);
};
/**
* 获得Key值所对应的Value值。
*
* @param key
* 属性配置文件的Key值
* @return Key值所对应的Value值
*/
public String getProperty(String key) {
return properties.getProperty(key);
}
/**
* 修改属性配置文件
*
* @param key
* 属性配置文件的Key值
* @param value
* 属性配置文件的value值
* @param propertyFilePath
* 属性配置文件的完整路径,
* 如:"com/pagination/config/PaginationConfig.properties"
* @return 修改成功返回true,失败则返回false
*/
public boolean setProperty(String key, String value) {
FileOutputStream fos = getFileOutputStream(filePath);
properties.setProperty(key, value);
boolean flag = false;
try {
properties.store(fos, null);
flag = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
close();
} finally {
fos = null;
}
}
}
return flag;
}
/**
* 移除属性配置文件中的某个属性
*
* @param key
* 属性配置文件的Key值
* @return 移除成功返回true,失败则返回false
*/
public boolean removeProperty(String key) {
FileOutputStream fos = getFileOutputStream(filePath);
properties.remove(key);
boolean flag = false;
try {
properties.store(fos, null);
flag = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
close();
} finally {
fos = null;
}
}
}
return flag;
}
/**
* 释放资源
*/
public void close() {
if (properties != null) {
properties.clear();
}
}
/**
* 返回加载了配置文件的Properties对象
*
* @param propertyFilePath
* 属性配置文件的完整路径,
* 如:"com/pagination/config/PaginationConfig.properties"
* @return java.util.Properties
*/
private Properties getProperties(String propertyFilePath) {
Properties properties = new Properties();
InputStream is = null;
try {
is = RWProperties.class.getClassLoader().getResourceAsStream(
propertyFilePath);
properties.load(is);
} catch (NullPointerException e) {
e.printStackTrace();
close();
} catch (IOException e) {
e.printStackTrace();
close();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
is = null;
}
}
}
return properties;
}
/**
* 返回已经加载了配置文件的文件输出流
*
* @param propertyFilePath
* 属性配置文件的完整路径,
* 如:"com/pagination/config/PaginationConfig.properties"
* @return java.io.FileOutputStream
*/
private FileOutputStream getFileOutputStream(String propertyFilePath) {
FileOutputStream fos = null;
String filePath = null;
filePath = RWProperties.class.getClassLoader().getResource(
propertyFilePath).getFile();
filePath = filePath.replaceFirst("/", "");
// 如果URL地址中含有空格,则空格会被"%20"替换,所以要将它替换回来
filePath = filePath.replaceAll("%20", " ");
try {
fos = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
close();
}
return fos;
}
}
二、
package js.thread;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* アプリ共通ユーティリティクラスです。
* @author
* @version $Revision: 1.2 $
*/
public final class Util {
/**
* コンストラクタ。<BR>
* <BR>
*/
private Util() {
}
/**
* 指定したプロパティファイルを読み込む。<BR>
* <BR>
* 引数のクラス置く場所で、引数のファイルをプロパティファイルとして読み込む。<BR>
* @param strFile プロパティファイル名
* @param cls プロパティファイル置く場所(クラス置く場所と同様)
* @return Properties 読み込むプロパティ
* @throws IOException 読み込む異常の場合
*/
@SuppressWarnings ("unchecked")
public static Properties loadProp(String strFile, Class cls)
throws IOException {
Properties pReturn = new Properties();
InputStream input = cls.getClassLoader().getResourceAsStream(strFile);//classpath root
//InputStream input1 = cls.getResourceAsStream(strFile);//プロパティファイル置く場所(クラス置く場所と同様)
if (input == null) {
return null;
}
try {
pReturn.load(input);
} finally {
try {
input.close();
} catch (IOException ioExp) {
// do nothing
}
}
return pReturn;
}
/**
* 指定したプロパティファイルを読み込む。<BR>
* <BR>
* 引数のファイルをプロパティファイルとして読み込む。<BR>
* @param strFile プロパティファイル名
* @return Properties 読み込むプロパティ
* @throws IOException 読み込む異常の場合
*/
public static Properties loadProp(String strFile) throws IOException {
Properties pReturn = new Properties();
InputStream input = new FileInputStream(strFile);
try {
pReturn.load(input);
} finally {
try {
input.close();
} catch (IOException ioExp) {
// do nothing
}
}
return pReturn;
}
/**
* 文字列空白チェック関数<BR>
* <BR>
* 引数の文字列が空白(連続空白も含む)、またはnullの場合、trueを返す。<BR>
* それ以外の場合はfalseを返す。
* @param strParam チェック対象文字列
* @return 文字列空白判定結果(true:空白 false:空白ではない)
*/
public static boolean isStrNull(String strParam) {
if (strParam == null) {
return true;
}
if ("".equals(strParam.trim())) {
return true;
}
return false;
}
/**
* 指定文字列から改行を取り外す。<BR>
* @param src String 文字列
* @return String 改行を取り外された文字列
*/
public static String removeLineBreaks(String src) {
String ret = src;
if (ret != null) {
ret = ret.replace("\r\n", " ");
ret = ret.replace("\r", " ");
ret = ret.replace("\n", " ");
}
return ret;
}
}
package js.thread;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public abstract class AbstractEnvironment {
/** 空白文字列 */
public static final String STR_BLANK = "";
/** 文字コード変換から */
private static final String ENCODE_FROM = "iso-8859-1";
/** 文字コード変換へ */
private static final String ENCODE_TO = "UTF-8";
/** 異常メッセージ */
private static final String FAIL = "設定ファイル読み込む失敗";
/** 設定ファイル */
protected Properties prop;
/** 文字コード変換 */
boolean isNeedCodeChg = true;
/**
* コンストラクタ<BR>
* <BR>
* 引数より、設定ファイルの内容を読み込む。<BR>
* 引数の「cls」がnullの場合、「strFile」を直接ファイルシステムから読み込む、<BR>
* 引数の「cls」がnullではないの場合、「cls」のクラスから「strFile」のリソースをシステムから読み込む。<BR>
* @param strFile 設定ファイル
* @param cls クラス
* @throws EnvironmentException 設定ファイル読み込む異常の場合
*/
@SuppressWarnings ("unchecked")
protected AbstractEnvironment(String strFile, Class cls)
throws RuntimeException {
try {
if (strFile == null) {
throw new RuntimeException(FAIL);
}
if (cls == null) {
prop = Util.loadProp(strFile);
isNeedCodeChg = false;
} else {
prop = Util.loadProp(strFile, cls);
}
if (prop == null) {
throw new RuntimeException(FAIL);
}
} catch (IOException ioExp) {
throw new RuntimeException(FAIL);
}
}
/**
* 引数の設定キーより、設定値を取得する。<BR>
* 設定キーが存在しない場合、空白を返す。
* @param strKey 設定キー
* @return String 設定値
*/
public String getValue(String strKey) {
return getValueByParam(strKey, null);
}
/**
* 引数の設定キーと設定値に埋め込み文字列より、設定値を取得する。<BR>
* 埋め込み文字列の置換パターンは{n}となる、「n」が0からの正数字となります。<BR>
* メモリ上に保存するメッセージをUTF-8に変換し、返す。<BR>
* 設定キーが存在しない場合、空白を返す。
* @param strKey 設定キー
* @param strArgs 設定値に埋め込み文字列
* @return String 埋め込み文字列置換後の設定値
*/
public String getValueByParam(String strKey, String[] strArgs) {
String strValue = prop.getProperty(strKey);
if (Util.isStrNull(strValue)) {
strValue = STR_BLANK;
} else {
if (isNeedCodeChg) {
try {
strValue = new String(strValue.getBytes(ENCODE_FROM),
ENCODE_TO);
} catch (UnsupportedEncodingException endExp) {
strValue = FAIL;
}
}
}
if (strArgs != null) {
for (int i = 0; i < strArgs.length; i++) {
if (strArgs[i] == null) {
strValue = strValue.replace("{" + i + "}", STR_BLANK);
} else {
strValue = strValue.replace("{" + i + "}", Util
.removeLineBreaks(strArgs[i]));
}
}
}
return strValue;
}
}
package js.thread;
/**
* システム設定値を提供する共通用クラスです。<br>
* <br>
* 運用時に、外部ファイルに設定されたシステム設定値を提供する。<br>
* <p>
* 用例:<br>
* Setting.init();<br>
* <br>
* String strSetting = Setting.getInstance().getValue("システム設定キー");<br>
* </p>
* @author
* @version $Revision: 1.2 $
*/
public final class Setting extends AbstractEnvironment {
/** システム設定シングルトン */
private static AbstractEnvironment environment;
/**
* コンストラクタ<BR>
* <BR>
* 引数より、システム設定ファイルの内容を読み込む。<BR>
* @param strFile システム設定ファイル
* @throws EnvironmentException システム設定ファイル読み込む異常の場合
*/
private Setting(String strFile) throws RuntimeException {
super(strFile, Setting.class);
}
/**
* システム設定クラスを初期化する。<BR>
* <BR>
* 引数より、システム設定ファイルの内容を読み込む。<BR>
* 設定ファイルからUTF-8で保存する内容をiso-8859-1でメモリ上に保存する。
* @param strFile システム設定ファイル
* @throws EnvironmentException システム設定ファイル読み込む異常の場合
*/
public static void init(String strFile) throws RuntimeException {
environment = new Setting(strFile);
}
/**
* システム設定の実例クラスを取得する。<BR>
* @return Setting 実例クラス
*/
public static AbstractEnvironment getInstance() {
return environment;
}
}
package js.thread;
public class TestProperties {
public static void main(String[] args) {
Setting.init("messageResource_en_US.properties");
AbstractEnvironment s = Setting.getInstance();
System.out.println(s.getValue("a"));
}
}