关闭

读取中文Properties文件

标签: propertiesstringexceptionclassloaderwarningsstream
1513人阅读 评论(0) 收藏 举报
分类:

    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("姓名")+"=========");

    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    最新评论
    理财参考