java.util.ResourceBundle 和java.util.properties 读取配置文件区别



java.util.ResourceBundle 和java.util.properties 读取配置文件区别

 

这两个类都是读取properties格式的文件的,而Properties同时还能用来写文件。  
   
  Properties的处理方式是将其作为一个映射表,而且这个类表示了一个持久的属性集,他是继承HashTable这个类。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格式的编码,否则不能正确识别。

   2.用法:

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();


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很遗憾,java.util.ResourceBundle 并不支持直接处理 JSON 格式的数据,它主要用于处理属性文件(即 .properties 文件)。如果需要处理 JSON 数据,可以考虑使用其他第三方库,比如 Jackson、Gson 等。 以 Jackson 为例,以下是一个处理 JSON 数据的示例: ```java import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonResourceBundle { private final ObjectMapper mapper = new ObjectMapper(); public ResourceBundle getBundle(String baseName) { return new JsonResourceBundleImpl(baseName); } private class JsonResourceBundleImpl extends ResourceBundle { private final JsonNode node; public JsonResourceBundleImpl(String baseName) { try { String jsonStr = // 从文件或其他来源中读取 JSON 字符串 node = mapper.readTree(jsonStr); } catch (IOException e) { throw new IllegalArgumentException("Invalid JSON format", e); } } @Override protected Object handleGetObject(String key) { JsonNode valueNode = node.get(key); if (valueNode == null) { return null; } if (valueNode.isValueNode()) { return valueNode.asText(); } // 如果需要支持多层嵌套,可以递归处理 throw new IllegalArgumentException("Invalid JSON format"); } @Override public Enumeration<String> getKeys() { return node.fieldNames(); } } } ``` 使用示例: ```java ResourceBundle bundle = new JsonResourceBundle().getBundle("messages.json"); String greeting = bundle.getString("greeting"); System.out.println(greeting); // 输出:Hello, world! ``` 其中 `messages.json` 文件内容如下: ```json { "greeting": "Hello, world!", "farewell": "Goodbye!" } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值