/*
* Created on Apr 8, 2010
* PropertiesTools.java
*
* Copyright 2004 Hintsoft, LTD. All rights reserved.
* HINTSOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
* $Id: PropertiesTools.java,v 1.2 2012/02/06 07:19:57 hcc Exp $
* $Author: ds
* $Revision: 1.2 $
*
*/
package base.util;
/**
* @author huangcc
*
*/
public class PropertiesTools {
public static BundleConfig config = null;
public static BundleConfig db = null;
public static BundleConfig idType = null;
public static BundleConfig serialComm = null;
public static BundleConfig lucenePath = null;
public static BundleConfig ver = null;
static {
try {
config = new BundleConfig("police");
idType = new BundleConfig("idType");
serialComm = new BundleConfig("SerialComm");
lucenePath = new BundleConfig("lucenePath");
ver = new BundleConfig("ver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getConfigProperties(String key, String def) {
String val = config.getString(key, def);
return val;
}
public static int getConfigProperties(String key, int def) {
int val = config.getInt(key, def);
return val;
}
public static String getConfigProperties(String key) {
String val=null;
if(StringUtils.isNotEmpty(key)){
try {
val = config.getString(key);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return val;
}
public static String getConfigPropertiesByIdType(String key) {
String val = idType.getString(key);
return val;
}
public static String getPathTypeByConfigProperties(String key) {
String val = config.getString(key);
String systemType = System.getProperty("os.name");
if (systemType.startsWith("Windows")) {
val = "D:" + val;
}
return val;
}
public static String[] getConfigPropertiesByArr(String key, String regex) {
String val = config.getString(key);
if (StringUtils.isEmpty(val)) {
return null;
}
return val.split(regex);
}
public static int getConfigPropertiesIntValue(String key) {
int val = config.getInt(key);
return val;
}
public static int getSerialCommProperties(String key, int def) {
int val = serialComm.getInt(key, def);
return val;
}
public static String getSerialCommProperties(String key) {
String val = serialComm.getString(key);
return val;
}
public static String getLucenePathProperties(String key) {
String val = lucenePath.getString(key);
return val;
}
public static String getLucenePathProperties(String key, String def) {
String val = lucenePath.getString(key, def);
return val;
}
public static String getIdTypeImgPath(String idTypeId) {
String path = getConfigPropertiesByIdType("path");
String idTypePath = getConfigPropertiesByIdType(idTypeId);
if (StringUtils.isEmpty(idTypePath)) {
// TODO 如果为空取个共用图片
idTypePath = "/images/idtype/ico_default01.gif";
} else {
idTypePath = path + idTypePath;
}
return idTypePath;
}
public static String getVerProperties(String key) {
String val=null;
if(StringUtils.isNotEmpty(key)){
val = ver.getString(key);
}
return val;
}
}
----相关类: BundleConfig
package base.util;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author jiangjianqiang
* @version 1.0, 2006-6-6
*/
public class BundleConfig {
public static final String CONFIG_FILENAME = "config";
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
private ResourceBundle resourceBundle;
/**
* 默认构造函数
*/
public BundleConfig() throws Exception {
this(CONFIG_FILENAME);
}
public BundleConfig(String configFileName) throws Exception {
logger.debug(configFileName);
try {
// 判断配置文件名称是否为空
if (configFileName != null) {
// 配置文件名称不为空
resourceBundle = ResourceBundle.getBundle(configFileName);
} else {
// 配置文件为空,读取系统默认配置文件名称
resourceBundle = ResourceBundle.getBundle(CONFIG_FILENAME);
}
} catch (MissingResourceException ex) {
// 读取配置文件错误,抛出异常
logger.error(ex.getMessage());
//throw new Exception(ex.getMessage());
}
}
public boolean getBoolean(String key) {
return getBoolean(key, false);
}
/**
* 根据Key值从配置文件中读取对应的参数boolean值,如果参数值为空,则返回默认值:defaultValue
*/
public boolean getBoolean(String key, boolean defaultValue) {
String stringValue = getString(key);
if (stringValue != null) {
return "TRUE".equalsIgnoreCase(stringValue);
} else {
return defaultValue;
}
}
/**
* 根据Key值从配置文件中读取对应的参数int值,返回默认值-1
*
* @param String
* key
* @return int
*/
public int getInt(String key) {
return getInt(key, 10);
}
/**
* 根据Key值从配置文件中读取对应的参数int值,如果参数值为空,则返回默认值:defaultValue
*
* @param String
* key int defaultValue
* @return int
*/
public int getInt(String key, int defaultValue) {
int intValue = -1;
String stringValue = getString(key);
// 判断配置文件中读取的字符串是否为空
if (stringValue != null) {
try {
intValue = Integer.parseInt(stringValue);
} catch (NumberFormatException ex) {
// 将字符串转化为数字发生错误
intValue = defaultValue;
}
} else {
// 字符串为空
intValue = defaultValue;
}
return intValue;
}
/**
* 根据Key值从配置文件中读取对应的参数long值
*
* @param String
* key
* @return long
*/
public long getLong(String key) {
return getLong(key, -1l);
}
/**
* 根据Key值从配置文件中读取对应的参数long值,如果参数值为空,则返回默认值:defaultValue
*
* @param String
* key long defaultValue
* @return long
*/
public long getLong(String key, long defaultValue) {
long longValue = -1l;
String stringValue = getString(key);
// 判断配置文件中读取的字符串是否为空
if (stringValue != null) {
try {
longValue = Long.parseLong(stringValue);
} catch (NumberFormatException ex) {
// 将字符串转化为数字发生错误
longValue = defaultValue;
}
} else {
// 字符串为空
longValue = defaultValue;
}
return longValue;
}
/**
* 根据Key值从配置文件中读取对应的参数String值,发生错误返回空值
*
* @param String
* key
* @return String
*/
public String getString(String key) {
// logger.debug("key" + key);
return getString(key, null);
}
/**
* 根据Key值从配置文件中读取对应的参数String值,如果参数值为空,则返回默认值:defaultValue
*
* @param String
* key String defaultValue
* @return String
*/
public String getString(String key, String defaultValue) {
String stringValue = null;
try {
stringValue = resourceBundle.getString(key);
} catch (Exception ex) {
// 读取配置文件错误,返回默认值
stringValue = defaultValue;
}
return stringValue;
}
}
附带说明:
myres_zh_CN.properties
myres.properties :
aaa=good
bbb=thanks
-------------------------------
myres_en_US.properties :
aaa=good
bbb=thanks
-----------------------------
myres_zh_CN.properties :
aaa=\u4F60\u597D native2ascii工具将 "你好" 为转码\u4F60\u597D
bbb=\u8C22\u8C22 native2ascii工具将 "谢谢" 为转码\u8C22\u8C22
-----测试代码:
import java.util.Locale;
import java.util.ResourceBundle;
/**
* 国际化资源绑定测试
* @author DS
*
*/
public class TestResourceBundle {
public static void main(String[] args) {
//java.util.Locale.Locale(String language, String country)
//加载 myres_zh_CN.properties
Locale locale1 = new Locale("zh", "CN"); //语言:中文,国家:中国
System.out.println("resb1, ISO 语言代码:"+locale1.getLanguage());
System.out.println("resb1, ISO 国家代码:"+locale1.getCountry());
System.out.println("适合向用户显示的语言名:"+locale1.getDisplayLanguage());
System.out.println("适合向用户显示的国家名: "+locale1.getDisplayCountry());
System.out.println("适合向用户显示的语言名(国家名):"+locale1.getDisplayName());
System.out.println(" 测试Locale结束-------" );
ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);
System.out.println(resb1.getString("aaa"));
//因为 Locale.getDefault() 所以加载 myres_zh_CN.properties ,而 不会加载myres.properties
ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault()); //默认:语言:中文,国家:中国
System.out.println("resb2, ISO 语言代码:"+locale1.getLanguage());
System.out.println("resb2, ISO 国家代码:"+locale1.getCountry());
System.out.println(resb2.getString("aaa"));
//myres_en_US.properties
Locale locale3 = new Locale("en", "US"); //语言:英文,国家:美国
System.out.println("resb3, ISO 语言代码:"+locale1.getLanguage());
System.out.println("resb3, ISO 国家代码:"+locale1.getCountry());
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
//打印------------------- :
/**
resb1, ISO 语言代码:zh
resb1, ISO 国家代码:CN
适合向用户显示的语言名:中文
适合向用户显示的国家名: 中国
适合向用户显示的语言名(国家名):中文 (中国)
测试Locale结束-------
你好
resb2, ISO 语言代码:zh
resb2, ISO 国家代码:CN
你好
resb3, ISO 语言代码:zh
resb3, ISO 国家代码:CN
good
*/
//总结:myres_zh_CN.properties 、myres.properties、myres_en_US.properties
/**
* 1、当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties;
* 在英文操作系统下,如果myres_en_US.properties、myres.properties两个文件都存在,则优先会使用myres_en_US.properties。
* 2、当在中文操作系统下,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。
* 在英文操作系统下,当myres_en_US.properties不存在时候,会使用默认的myres.properties。
* 3、没有提供语言和地区的资源文件是系统默认的资源文件。
* 4、资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。
* 5、转换方法是通过JDK自带的工具native2ascii.
*
*/
}
}
-----------------------------------
三、认识Locale
Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的 操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。
Locale(String language, String country)
Locale(String language, String country, String variant)
如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果: