读取中文Properties文件

原创 2006年06月15日 15:08:00

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

    }

}

Java读取properties配置文件时,中文乱码解决方法

转自:http://pig345.iteye.com/blog/725974 碰到了用java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的...

Java读取properties配置文件时,中文乱码解决方法

转自:http://pig345.iteye.com/blog/725974 碰到了用Java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的现象, Java代...

spring使用@Value标签读取.properties文件的中文乱码问题的解决

beginbeginspring使用@Value标签读取*.properties文件的中文乱码问题的解决spring 中文乱码 @Value *.properties在spring中我们常常使用.pr...
  • J3oker
  • J3oker
  • 2016年12月23日 14:33
  • 5799

JSTL读取properties文件时的中文问题

中文内容必须经过Unicode编码,默认的properties文件是ISO8859-1编码,不能存储中文。 可以使用jdk自带的native2asii.exe工具,只要环境变量设置正确,可以直接使用...

Eclipse中properties文件读取时中文乱码、指定更新、写入中文变unicode码的解决方法

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import...

SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码

在springMVC中,后台使用hibernate-validator做参数校验的时候,用properties文件配置了校验失败的错误信息,发现回显给前端页面的时候中文错误信息显示乱码。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:读取中文Properties文件
举报原因:
原因补充:

(最多只允许输入30个字)