PropertiesTools -- Properties工具类

/*
 * 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;
 }
}

 

附带说明:

java.util.ResourceBundle使用详解
 
一、认识国际化资源文件
 
这个类提供软件国际化的捷径。通过此类,可以使您所编写的程序可以:
         轻松地本地化或翻译成不同的语言
         一次处理多个语言环境
         以后可以轻松地进行修改,支持更多的语言环境
 
说的简单点,这个类的作用就是读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
 
使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties
如果是默认的,直接写为: 自定义名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
 
当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。
 
没有提供语言和地区的资源文件是系统默认的资源文件。
资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.
 
二、实例
 
定义三个资源文件,放到src的根目录下面(必须这样,或者你放到自己配置的calsspath下面。

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:
 Locale(String language)
 Locale(String language, String country)
 Locale(String language, String country, String variant)
 
创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。

 
四、中文资源文件的转码工具 native2ascii (jdk附带的有这个工具)
 
这个工具用法如下:

 

 

如果觉得麻烦,可以直接将中文粘贴到里面,回车就可以看到转码后的结果:

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值