好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

聊聊ResourceBundle和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:

?

1

2

ResourceBundle conf= ResourceBundle.getBundle( "config/fnconfig/fnlogin" );

String value= conf.getString( "key" );

Properties:

?

1

2

3

4

5

6

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文件及乱码处理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

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 "" ;

}

}}

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

原文链接:https://www.cnblogs.com/Syria/p/6298345.html

查看更多关于聊聊ResourceBundle和properties读取配置文件的区别的详细内容...

  阅读:13次