用 ResourceBundle 读取Properties文件以及乱码处理

package read;

import java.util.ResourceBundle;
/**
 * 属性文件工厂类
 * @author W
 * @version V1.0
 * @date 2013-4-17
 */
public interface ReadPropertiesFactory {
	public ResourceBundle getErrorResource();
	
}


================================================


package read;

import java.util.ResourceBundle;

/**
 * 
 * @author 
 * @version V1.0
 * @date 2013-5-13
 */
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;

/**
 * 
 * @author 
 * @version V1.0
 * @date 2013-4-17
 */
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 "";
}
}}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值