ResourceBundle
一般来说,ResourceBundle类通常是用于针对不同的语言来使用的;
如果程序中的属性文件只是一些配置,并不是针对多国语言,那么使用Properties。
ResourceBundle,其作用是国际化,他的国际化文件也是.properties文件,只不过不用Properties类解析,而是用的ResourceBundle类来解析而已。
这个被ResourceBundle解析的.properties文件必须有命名规范: basename_语言代码_国家码.properties
如果是默认的,直接写为:basename.properties
系统默认的资源文件:只有basename,没有提供语言和地区。
ResourceBundle.getBundle():
/**
* return a resource bundle for the given base name and the default locale: Locale.getDefault()
*/
public static final ResourceBundle getBundle(String baseName);
/**
* @return a resource bundle for the given base name and locale
*/
public static final ResourceBundle getBundle(String baseName,
Locale locale);
Locale
Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。
使用此类中的构造方法来创建 Locale:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
Java读取Properties文件的方法
1. 使用java.util.Properties类的load()方法
1.1 使用InputSteam系列方法
InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
1.2 使用class变量的getResourceAsStream()方法
InputStream in = Properties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
2. 使用java.util.ResourceBundle类的getBundle()方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());