Java中使用ResourceBundle获取属性文件信息

洼地云 tuoyidashi.png

一、介绍:

使用java.util.ResourceBundle这个类可以实现在Java中轻松的读取资源属性文件(.properties),然后获取属性文件(.properties)文件的内容。

使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties
如果是默认的,直接写为:自定义名.properties
比如:

myres_en_US.properties
myres_zh_CN.properties
myres.properties

当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties,当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。

二、示例

1.首先定义一个资源文件jdbc.properties,放到src的根目录下面(必须这样,或者你放到自己配置的classpath下面)。代码结构如下:

2.使用Java代码获取属性文件的信息

import java.util.ResourceBundle;

/**
 * Created by https://www.4spaces.org on 2016/5/29.
 */
public class ResourceBundleDemo {
    public static void main(String[] args) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
        System.out.println(resourceBundle.getString("jdbc.password"));
    }
}
赞(0)
未经允许禁止转载:优米格 » Java中使用ResourceBundle获取属性文件信息

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册