iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >聊聊ResourceBundle和properties读取配置文件的区别
  • 243
分享到

聊聊ResourceBundle和properties读取配置文件的区别

2024-04-02 19:04:59 243人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Propertie

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

ResourceBundle 读取Properties文件及乱码处理


package read; 
import java.util.ResourceBundle;

public interface ReadPropertiesFactory {
	public ResourceBundle getErrorResource();	
} 
================================================  

package read; 
import java.util.ResourceBundle;
 

public class ReadPropertiesFactoryImpl implements ReadPropertiesFactory {
	private ResourceBundle errorResouce;
	
	public ResourceBundle getErrorResource() {
		if(errorResouce == null){
                     //只要读取properties的名称就可以了
			errorResouce = ResourceBundle.getBundle("errORMessage");
		}
		return errorResouce;
	} 	
}
===============================================
 
package util; 
import java.io.UnsupportedEncodingException; 

public class StringHanlder {
	public static String transformCodeIso8859Style(String code , String codeStyle) throws UnsupportedEncodingException{
		return new String(code.getBytes("ISO-8859-1"),codeStyle);
	}
	public static String transformCodeUtf8Style(String code , String codeStyle) throws UnsupportedEncodingException{
		return new String(code.getBytes("utf-8"),codeStyle);
	}
}
=========================================================================
errorMessage.properties文件中的属性
E01010024=查询数据异常!
=============================================================================
package www.man.comService;
import java.util.ResourceBundle;
import read.ReadPropertiesFactoryImpl;
public class TestService {public static void main(String[] args) {
String a= TestService.getErrorValue("E01010070");System.out.println(a);}
private static  ResourceBundle getErrorResource() {
ReadPropertiesFactoryImpl readPropertiesFactory =new ReadPropertiesFactoryImpl();
return readPropertiesFactory.getErrorResource();
}
public  static String getErrorValue(String key){
try{
return util.StringHanlder.transformCodeIso8859Style(getErrorResource().getString(key),"utf-8");
}catch(Exception e){
e.printStackTrace();return "";
}
}}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 聊聊ResourceBundle和properties读取配置文件的区别

本文链接: https://www.lsjlt.com/news/130928.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作