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

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

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

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

  1. /**
  2. * Gets a resource bundle using the specified base name, the default locale,
  3. * and the caller's class loader. Calling this method is equivalent to calling
  4. * <blockquote>
  5. * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
  6. * </blockquote>
  7. * except that <code>getClassLoader()</code> is run with the security
  8. * privileges of <code>ResourceBundle</code>.
  9. * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
  10. * for a complete description of the search and instantiation strategy.
  11. *
  12. * @param baseName the base name of the resource bundle, a fully qualified class name
  13. * @exception java.lang.NullPointerException
  14. * if <code>baseName</code> is <code>null</code>
  15. * @exception MissingResourceException
  16. * if no resource bundle for the specified base name can be found
  17. * @return a resource bundle for the given base name and the default locale
  18. */
  19. public static final ResourceBundle getBundle(String baseName)
  20. {
  21. return getBundleImpl(baseName, Locale.getDefault(),
  22. /* must determine loader here, else we break stack invariant */
  23. getLoader(),
  24. Control.INSTANCE);
  25. }

  1. ResourceBundle
  2. ResourceBundle conf= ResourceBundle.getBundle("config/fnconfig/fnlogin");
  3. String value= conf.getString("key");
  4.  
  5. Properties:
  6. Properties prop = new Properties();
  7. try {
  8. InputStream is = getClass().getResourceAsStream("xmlPath.properties");
  9. prop.load(is);
  10. //或者直接prop.load(new FileInputStream("c:/xmlPath.properties"));
  11. if (is != null) {
  12. is.close();
  13. }
  14. } catch (Exception e) {
  15. System.out.println( "file " + "catalogPath.properties" + " not found!\n" + e);
  16. }
  17. 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. SQL注入问题

    斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz),转载请注明出处! 背景:对于ibaits参数引用可以使用#和$两种写法,其中#写法会采用预编译方式,将转义 ...

  2. ffplay for mfc 代码备忘录

    在上传一个开源播放器项目ffplay for mfc.它会ffmpeg工程ffplay媒体播放器(ffplay.c)移植到VC环境,而使用MFC做一套接口.它可以完成一个播放器播放的基本流程的视频:解 ...

  3. FPGA开机状态

    最近调试FPGA电路时发现一个问题,我从来没有注意过.我们都知道Xilinx的FPGA有三种功率M引脚,这是为了让我们配置三个引脚FPGA装载机模式,什么是主要的字符串.从字符串.并行等.,该手册有. ...

  4. 构建工具maven

     构建工具maven  =UTF-8''Gradle Effective Implementation Guide.pdf: http://www.t00y.com/file/76854506 b ...

  5. Hadoop-2.2.0中文文档—— MapReduce下一代- 可插入的 Shuffle 和 Sort

    简单介绍 可插入的 shuffle 和 sort 功能,同意在shuffle 和 sort 逻辑中用可选择的实现类替换.这个情况的样例是:用一个不是HTTP的应用协议,如RDMA来 shuffle 从 ...

  6. 怎样设制 select 不可编辑 仅仅读

    1. <select style="width:195px" name="role"  id="role" onfocus=" ...

  7. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  8. JavaFX横幅类游戏开发 教训 游戏贴图

    上一节课,我们即将完成战旗Demo有了一个大概的了解.教训这,我们将学习绘制游戏地图. 由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了. 游戏地图绘 ...

  9. HDU 2008 数字统计

    号码值统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  10. topshelf和quartz

    topshelf和quartz内部分享 阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介 ...