java resourcebundle_Java - Properties和ResourceBundle类学习

一、前言

在项目的开发过程中,为了统一配置的管理,我们经常需要将一些配置信息根据环境的不同,配置在不同的properties中,然后从里面进行读取。而Properties类作为最基础也是最经常使用的类,通过本文我们来学习一下它的使用,然后再顺便学习下其他几种读取properties文件的方式。

二、Properties和ResourceBundle类

Properties表示一个持久的属性集,属性列表通过key-value的形式存在,并且key和value都是字符串。我们先来看一下它的继承结构:

1. Properties 继承结构

public

class Properties extends Hashtable {

/**

* A property list that contains default values for any keys not

* found in this property list.

*

* @serial

*/

protected Properties defaults;

public Properties() {

this(null);

}

public Properties(Properties defaults) {

this.defaults = defaults;

}

}

看到它的继承结构,就知道这个类已经存在好久了。该类继承自Hashtable,所以该类拥有Map的一切功能,所以Map的put或者putAll方法都可以使用。

不过由于Properties中的每个key及value都是字符串,所以官方强烈反对使用它们,因为这些方法允许key或者value不是字符串,如果在set或get操作的时候,不是字符串的话,则会提示异常,所以建议使用的则是诸如setProperty这些方法。

2. Properties常用方法

这里Map相关的方法就不介绍了,主要介绍下自定义的方法:

2.1 setProperty方法

public synchronized Object setProperty(String key, String value) {

return put(key, value);

}

底层通过Hashtable的put方法来实现,该方法的目的就是强制对属性的key和value都使用字符串的形式;

2.2 getProperties方法

public String getProperty(String key)

public String getProperty(String key, String defaultValue) {

String val = getProperty(key);

return (val == null) ? defaultValue : val;

}

获取属性列表中属性的key对应的值,第二个重载方法表示如果获取不到值返回参数中提供的默认值。

2.3 load方法

public synchronized void load(Reader reader) throws IOException

public synchronized void load(InputStream inStream) throws IOException

load方法表示从输入流(字节流和字符流)中读取属性列表到Properties中,读取的时候按照行进行读取。而有关读取行及相关转义的说明,可以参考对应的API文档,上面有详细的说明。

2.4 loadFromXML方法

public synchronized void loadFromXML(InputStream in)

throws IOException, InvalidPropertiesFormatException

从输入流中读取XML文件中的所有属性,注意XML文档必须有相应的DTD声明:

2.5 store方法

public void store(Writer writer, String comments)

throws IOException

public void store(OutputStream out, String comments)

throws IOException

和load的功能相反,将Properties的属性列表写入到输出流,其中参数表示对属性列表的说明。

2.6 storeToXML方法

public void storeToXML(OutputStream os, String comment)

throws IOException

public void storeToXML(OutputStream os, String comment, String encoding)

throws IOException

将属性写入到xml,并可以指定的编码格式。

2.7 propertyNames和stringPropertyNames方法

public Enumeration> propertyNames()

public Set stringPropertyNames()

两个方法都是返回Properties属性列表中所有key,前者返回所有枚举,后者返回类型是字符串,注意如果没有在主属性列表中找到同名的键,则在默认属性列表中进行查找。

2.8 list方法

public void list(PrintStream out)

public void list(PrintWriter out)

将属性列表输出到指定的输出流,这个方法对调试很有用。

3. ResourceBundle简介

ResourceBundle没有继承什么类,是一个单个的抽象类,该类可以说是国际化版的Properties,简单说就是可以根据本地化或语言的不同读取不同的配置文件,但要注意的一点是使用ResourceBundle读取的时候,properties的命名是有一定规范的:

名称_语言代码_国家代码.properties

// 如果是默认的

自定义名.properties

// 例如

myres_en_US.properties

myres_zh_CN.properties

myres.properties

当指定的Locale是CN的时候,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。

4. ResourceBundle常用方法

4.1 getBundle方法

ResourceBundle提供了多个重载的静态getBundle方法,用于获取资源文件,这里我们不多介绍,后续看实例即可:

public static final ResourceBundle getBundle(String baseName)

public static final ResourceBundle getBundle(String baseName,

Locale locale)

public static ResourceBundle getBundle(String baseName, Locale locale,

ClassLoader loader)

public static final ResourceBundle getBundle(String baseName,

Control control)

public static ResourceBundle getBundle(String baseName, Locale targetLocale,

ClassLoader loader, Control control)

4.2 getObject,getString,getStringArray方法

getString方法比较简单,就是根据key获取属性列表中key对应的value,key和value都是String;

getStringArray方法,用于获取字符串数组,返回值是字符串数组;

getObject方法,通用的获取方法,获取其他任何类型;

public final String getString(String key)

public final String[] getStringArray(String key)

public final Object getObject(String key)

4.3 keySet,getKeys,containsKey方法

这几个方法都比较简单,见名知义,其中getKeys表示返回key的枚举形式。

public Set keySet()

public abstract Enumeration getKeys();

public boolean containsKey(String key)

4.4 getBaseBundleName,getLocale方法

getBaseBundleName方法就是获取加载的资源文件的文件名的,getLocale获取本地化环境信息的。

public String getBaseBundleName()

public Locale getLocale()

4.5 clearCache方法

通过getBundle方法读取资源文件,获取ResourceBundle时是从缓存中获取的,如果已经缓存,工厂方法将多次返回相同的资源实例,而clearCache方法就是用于清除缓存的:

public static final void clearCache()

public static final void clearCache(ClassLoader loader)

4. 简单示例

接下来我们来简单看下这些方法的相关使用说明。

4.1 Properties 通过store方法写入对应的文件中

首先我们调用setProperty方法会将key-value存于内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

Properties properties = new Properties();

try {

OutputStream output = new FileOutputStream("cache.properties");

properties.setProperty("redis.server.address", "127.0.0.1");

properties.setProperty("redis.server.port", "6379");

properties.setProperty("redis.server.password", "");

properties.setProperty("redis.server.timeout", "2000");

properties.store(output, "缓存文件配置测试");

} catch (IOException io) {

io.printStackTrace();

} finally {

...

}

最终生成的cache.properties文件:

#缓存文件配置测试

#Sun Aug 19 12:27:05 CST 2018

redis.server.timeout=2000

redis.server.address=127.0.0.1

redis.server.password=

redis.server.port=6379

4.2 Properties使用load方法加载

同样,我们可以通过load方法将属性文件中的属性加载到Properties对象中,然后进行访问:

Properties properties = new Properties();

InputStream inputStream = null;

try {

inputStream = new FileInputStream("cache.properties");

properties.load(inputStream);

for (String key : properties.stringPropertyNames()) {

System.out.println(key + "=" + properties.getProperty(key));

}

} catch (IOException io) {

io.printStackTrace();

} finally {

...

}

最终结果:

redis.server.timeout=2000

redis.server.address=127.0.0.1

redis.server.password=

redis.server.port=6379

因为Properties其实是一个Map,所以Map的遍历方式也适用于Properties,也可也借助Properties的propertyNames方法来进行遍历:

Enumeration> e = properties.propertyNames();

while (e.hasMoreElements()) {

String key = (String) e.nextElement();

String value = properties.getProperty(key);

System.out.println(key + "=" + value);

}

4.3 ResourceBundle简单实例

我们先定义三个资源文件,放到src的根目录下面:

myres.properties

aaa=good

bbb=thanks

myres_en_US.properties

aaa=good

bbb=thanks

myres_zh_CN.properties

aaa=好

bbb=谢谢

添加测试代码:

public static void main(String[] args) {

Locale locale1 = new Locale("zh", "CN");

ResourceBundle resb1 = ResourceBundle.getBundle("cache", locale1);

System.out.println(resb1.getString("aaa"));

ResourceBundle resb2 = ResourceBundle.getBundle("cache", Locale.getDefault());

System.out.println(resb1.getString("aaa"));

Locale locale3 = new Locale("en", "US");

ResourceBundle resb3 = ResourceBundle.getBundle("cache", locale3);

System.out.println(resb3.getString("aaa"));

}

output:

good

这里需要注意下,通过getBundle方法来获取的时候,参数不用加properties后缀,只需要文件名就可以了,并且默认访问的路径是src,如果文件保存在其他目录,记得修改到对应的目录。

5. 其他读取properties文件的方法

5.1 Properties其他获取InputStream的方法

在这里,其实读取properties都是通过Properties来实现的,不过不同的是获取InputStream流的方式,我们来看一下各种获取InputStream流的方式:

从File文件获取:

InputStream inputStream = new FileInputStream(new File ("cache.properties"));

根据ClassLoader的getResourceAsStream方法来获取。其中该方法又分为两种方式:

Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其实只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源;

Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由ClassLoader获取资源;

ServletContext.getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,当然这和具体的容器实现有关;

InputStream inputStream = PropertiesTest.class.getResourceAsStream("cache.properties");

通过URL来获取:

URL url = new URL("path");

InputStream inputStream= url.openStream();

如果是Spring环境,还可以通过ResourceLoader的getResource得到Resource,然后通过Resource的getInputStream来得到流:

ResourceLoader resourceLoader = new DefaultResourceLoader();

Resource resource = resourceLoader.getResource("/config/cache.properties");

inputStream = resource.getInputStream();

5.2 ResourceBundle类相关方法

前面也已经简单介绍过,我们可以借助java.util.ResourceBundle的getBundle静态方法来获取资源实例:

Locale locale1 = new Locale("zh", "CN");

ResourceBundle resb1 = ResourceBundle.getBundle("cache", locale1);

另外,也可以借助实现类 PropertyResourceBundle 通过从输入流中进行读取:

inputStream = new FileInputStream("cache.properties");

ResourceBundle resource = new PropertyResourceBundle(inputStream);

不过如果有兴趣的话可以看下PropertyResourceBundle的构造方法,它其实是借助Properties和HashMap来实现的:

public PropertyResourceBundle (InputStream stream) throws IOException {

Properties properties = new Properties();

properties.load(stream);

lookup = new HashMap(properties);

}

private Map lookup;

三、总结

到这里,就基本介绍完了Properties和ResourceBundle类及如何读取properties文件,其实,介绍的都比较基础,需要注意的可能就两点吧:一是路径的问题,二是流的关闭和异常处理问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值