zoukankan      html  css  js  c++  java
  • ResourceBundle

    ResourceBundle本质上也是一个映射,但是它提供了国际化的功能。 
    假设电脑设置的地区是中国大陆,语言是中文 
    那么你向ResourceBundle(资源约束名称为base)获取abc变量的值的时候,ResourceBundle会先后搜索 
    base_zh_CN_abc.properties 
    base_zh_CN.properties 
    base_zh.properties 
    base.properties 
    文件,直到找到abc为止 

    相应的,在英国就会去找base_en_GB_abc.properties等。 
    因此,你只需要提供不同语言的资源文件,而无需改变代码,就达到了国际化的目的。 
    另外,在.properties里面,不能直接使用中文之类文字,而是要通过native2ascii转乘uxxxx这种形式
    附:
    1.编码问题:
    无论系统的默认编码是什么,ResourceBundle在读取properties文件时统一使用iso8859-1编码。
    因此,如果在默认编码为 GBK的系统中编写了包含中文的properties文件,经由ResourceBundle读入时,必须转换为GBK格式的编码,否则不能正确识别。src esources esourceBundle.properties

    username=hello
    pwd=root
    zhname=小明
    package resources;
    
    import java.util.ResourceBundle;
    
    public class ResourceBundleDemo {
        public static void main(String[] args) {
            //baseName是Properties文件在当前项目的相对全路径,不需要扩展名src
    esources
    esourceBundle.properties
            ResourceBundle resourceBundle = ResourceBundle.getBundle("resources/resourceBundle");
            String userName = resourceBundle.getString("username");
            String pwd = resourceBundle.getString("pwd");
            String zhName = resourceBundle.getString("zhname");
            System.out.println(String.format("UserName:%s,Pwd:%s,中文名:%s", userName, pwd,zhName));
        }
    }

    Output:
    UserName:hello,Pwd:root,中文名:???÷
    乱码原因:

    在.properties里面,不能直接使用中文之类文字,而是要通过native2ascii转乘uxxxx这种形式

        /**
         * Gets a resource bundle using the specified base name, the default locale,
         * and the caller's class loader. Calling this method is equivalent to calling
         * <blockquote>
         * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
         * </blockquote>
         * except that <code>getClassLoader()</code> is run with the security
         * privileges of <code>ResourceBundle</code>.
         * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
         * for a complete description of the search and instantiation strategy.
         *
         * @param baseName the base name of the resource bundle, a fully qualified class name
         * @exception java.lang.NullPointerException
         *     if <code>baseName</code> is <code>null</code>
         * @exception MissingResourceException
         *     if no resource bundle for the specified base name can be found
         * @return a resource bundle for the given base name and the default locale
         */
        public static final ResourceBundle getBundle(String baseName)
        {
            return getBundleImpl(baseName, Locale.getDefault(),
                     /* must determine loader here, else we break stack invariant */
                     getLoader(),
                     Control.INSTANCE);
        }



        ResourceBundle
        ResourceBundle conf= ResourceBundle.getBundle("config/fnconfig/fnlogin");
        String value= conf.getString("key");
     
    
        Properties:
        Properties prop = new Properties();
        try {
            InputStream is = getClass().getResourceAsStream("xmlPath.properties");
            prop.load(is);
            //或者直接prop.load(new FileInputStream("c:/xmlPath.properties"));
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {
            System.out.println( "file " + "catalogPath.properties" + " not found!
    " + e);
        }
        String value= prop.getProperty("key").toString();





    http://oyzzhou.iteye.com/blog/854796






  • 相关阅读:
    C#根据url生成唯一的key
    MyBatis基本配置和实践(四)
    MyBatis基本配置和实践(三)
    MyBatis基本配置和实践(二)
    MyBatis基本配置和实践(一)
    dbcp2、c3p0、druid连接池的简单配置
    HTTP长连接和短连接
    Java Web高性能开发
    三层构架 和 MVC 是什么?
    Docker bridge探索
  • 原文地址:https://www.cnblogs.com/softidea/p/4826153.html
Copyright © 2011-2022 走看看