读取中文Properties文件

    private String propFileName = null;
    //locale = language+country(比如:zh_CN)
    private Locale curLocale = null;

   public ResourceMessage(String baseName) {
       this(baseName, Locale.getDefault());
    }
    public ResourceMessage(String baseName ,Locale locale) {
        curLocale = locale;
        propFileName = FileOperator.getCurrentDir() + (baseName+"_"+curLocale+".properties");
        bundle = ResourceBundle.getBundle(baseName,curLocale);
    }
    public ResourceMessage(String baseName, String language, String country){
        this(baseName, new Locale(language,country));
    }

   //============================= Properties 操作 ========================================//
    public void updateProperty(String pKey,String pValue)throws Exception{
        Properties props = loadProperties();
        props.setProperty(pKey,pValue);
        storeProperties(props);
    }
    public void deleteProperty(String pKey) throws Exception{
     Properties props = loadProperties();
        props.remove(pKey);
        storeProperties(props);
    }
    public void clearProperty() throws Exception{
     Properties props = loadProperties();
     for (Enumeration enum=props.keys();enum.hasMoreElements();){
            props.remove(enum.nextElement());
     }
        storeProperties(props);
    }
    public void addProperty(String pKey,String pValue) throws Exception{
       Properties props = loadProperties();
       if (props.containsKey(pKey)){
       throw new Exception("已经存在该属性名:"+pKey);
       }
       StringBuffer buffer = new StringBuffer(pKey);
       buffer.append("=");
       buffer.append(pValue);
       FileOperator.appendChars(propFileName, buffer.toString());
    }

    public String toString(){
        return propFileName;
    }

    private Properties loadProperties() throws FileNotFoundException,IOException{
       String[] content = Tools.splitStr(new String(FileOperator.readChars(propFileName)),"/n");
       Properties props = new Properties();
       String propKey;
       for (int i=0; i<content.length; i++){
           int index = content[i].indexOf("=");
           if (index != -1){
              propKey = content[i].substring(0, index).trim();
              props.put(propKey, content[i].substring(index + 1).trim());
           }
       }
       return props;
    }
    private void storeProperties(Properties props) throws FileNotFoundException,IOException{
       StringBuffer content = new StringBuffer("#"+new java.util.Date());
       String keyName = null;
       for(Enumeration keys=props.keys();keys.hasMoreElements();){
          keyName = (String)keys.nextElement();
          content.append("/n");
          content.append(keyName);
          content.append("=");
          content.append(props.getProperty(keyName));
       }
       char[] chars = new char[content.length()];
       content.getChars(0,chars.length, chars,0);
       FileOperator.writeChars(propFileName, chars);
    }

    private String getProperty(String pKey){
       String message = "";
       try{
          message = bundle.getString(pKey);
       }catch(Exception e){
          System.err.println("Warning: Can't find the key '"+pKey+"' in "+ toString());
       }
       return message;
    }
    public String readProperty(String pKey){
       String message = "";
       try{
          Properties props = loadProperties();
          message = props.getProperty(pKey, "");
       }catch(Exception e){
          System.err.println("Error: "+e.getMessage());
       }
       if (message.equals("")){
          System.err.println("Warning: Can't find the key '"+pKey+"' in "+ toString());
       }
       return message;
    }
    //
    public InputStream readFileInJar(String pathInJar){
       InputStream stream = (InputStream)java.security.AccessController.doPrivileged(
             new java.security.PrivilegedAction() {  //接口
                 ClassLoader loader =  ResourceMessage.class.getClassLoader();
                 public Object run(){
                    if (loader != null){
                       return loader.getResourceAsStream("");
                    }else{
                       return ClassLoader.getSystemResourceAsStream("");
                    }
                 }
             }
          );
       return stream;
    }
    public InputStream get(){
       InputStream stream = (InputStream)java.security.AccessController.doPrivileged(
            new java.security.PrivilegedAction() {
                   ClassLoader loader =  ResourceMessage.class.getClassLoader();
                   public Object run() {
                       System.out.println("========="+loader.getResource("TestRes_zh_CN.properties"));
                       if (loader != null) {
                           return loader.getResourceAsStream("TestRes_zh_CN.properties");
                       } else {
                           return ClassLoader.getSystemResourceAsStream("TestRes_zh_CN.properties");
                       }
                   }
               }
           );
       return stream;
    }


    public static void main(String args[])throws Exception{
        ResourceMessage handler = new ResourceMessage("TestRes");
//        Object[] warnings = {new java.util.Date(),new java.util.Date(),"1212"};
//        System.out.println(handler.getMessage("warning", warnings));
//        Object[] indicates = {"熊水林","zhonglf"};
//        System.out.println(handler.getMessage("indicate", indicates));
        handler.get();
        System.out.println(handler.getProperty("warning"));
        System.out.println(handler.readProperty("姓名")+"=========");

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值