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\resources\resourceBundle.properties

username=hello
pwd=root
zhname=小明
package resources;

import java.util.ResourceBundle;

public class ResourceBundleDemo {
public static void main(String[] args) {
//baseName是Properties文件在当前项目的相对全路径,不需要扩展名src\resources\resourceBundle.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!\n" + e);
}
String value= prop.getProperty("key").toString();

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

ResourceBundle的更多相关文章

  1. 错误 java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle

    出现错误: java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle 百度搜 ...

  2. Java ResourceBundle类的使用

    1.使用ResourceBundle读取配置文件 #数据库配置信息: DRIVER=com.mysql.jdbc.Driver URL=jdbc:mysql://localhost:3306/dmo ...

  3. 国际化,java.util.ResourceBundle使用详解

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

  4. 【转】java.util.ResourceBundle使用详解

    原文链接:http://lavasoft.blog.51cto.com/62575/184605/ 人家写的太好了,条理清晰,表达准确.   一.认识国际化资源文件   这个类提供软件国际化的捷径.通 ...

  5. java.util.ResourceBundle使用详解

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

  6. 资源绑定ResourceBundle

    package com.init; import java.util.ResourceBundle; public class Resources { /** * @param args */ pub ...

  7. [原创]java WEB学习笔记51:国际化 概述,API 之 locale类,dataFormat类,numberFormat类, MessageFormat类,ResourceBundle 类

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. 使用ResourceBundle访问资源文件(properties)帮助类

    import java.util.ResourceBundle; /** * 读取properties文件的帮助类 * @author */ public class PropertiesUtil { ...

  9. javaWEB国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

    DateFormat:格式化日期的工具类,本身是一个抽象类: NumberFormat:格式化 数字 到 数字字符串,或货币字符串的字符类; MessageFormat: 可以格式化模式字符串,模式字 ...

  10. java.util.ResourceBundle使用详解(转)

    java.util.ResourceBundle使用详解   一.认识国际化资源文件   这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译成不同的 ...

随机推荐

  1. 白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》串行4(场景变化)

    作为一个真正的利用价格值应用,首先,你应该至少有两页,通过切换页面来实现很多其他互动.比如手机人人网,首先,打开后进入登录页面,将有登录后,新的东西.然后拉左侧面板.你可以看到相册.私人信息.像其他应 ...

  2. Unreal Engine 4 创建Destructible Mesh(可破坏网格)

    Unreal Engine 4的物理引擎用的是PhysX. 支持网格破坏.布料.物理粒子等,非常强大.曾经须要编码才干完毕的工作,在Unreal Engine 4 中仅仅须要拖拖拽拽就完毕了,非常方便 ...

  3. [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换

    其中这个 twrp 2.8.4 在18号的下午已经编译好了. 经历了2个小时的代码修改,再经过后期的调试,中英文双语版本的twrp出炉了. 下面上几张图: 图片1:为英文界面 图片2: 图片3:中文界 ...

  4. android 中国通信乱码问题

    1.要解决中文乱码问题.首先得了解什么是字符编码 计算机要处理各种字符,就须要将字符和二进制内码相应起来,这样的相应关系就是字符编码. 要制定字符编码首先要确定字符集,并将 字符集内的字符排序.然后和 ...

  5. javascript焦点图(能够自己主动切换 )

    /* 思路总结: 1.实现图片滚动的function.鼠标经时候获取当前li的index.设置ndex自己主动递增的函数.实现淡入淡出效果的函数 2.整个实现效果一传递index为主线 3.我的编写代 ...

  6. Android多点触控(图片的缩放Demo)

    本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理.须要实现OnTouchListener接口,重写当中的onTouch方法. 实现效果图:       源码: 布局文 ...

  7. Ejb in action(两)——示例入门

    在前面的文章中,,我们推出Ejb相关概念.在此之前展开的阐述,我给大家Ejb示例.加深印象. 开发环境:eclipse 应用server:jboss 1.服务端程序 在Eclipse中创建一个Ejb项 ...

  8. ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]

    门户:problemCode=3635">ZOJ 3635 Cinema in Akiba Time Limit: 3 Seconds      Memory Limit: 65536 ...

  9. 一篇非常全面的freemarker教程

    copy自http://demojava.iteye.com/blog/800204 下面内容所有是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...

  10. c# 操作 MongoDB 的 第三方类库 MongoRepository

    https://github.com/RobThree/MongoRepository 文档 https://github.com/RobThree/MongoRepository/wiki/Docu ...