读取properties文件-PropertyResourceBundle
来源:互联网 发布:淘宝怎么拒签退款流程 编辑:程序博客网 时间:2024/05/16 01:15
- import java.util.PropertyResourceBundle;
- import java.util.ResourceBundle;
- import org.apache.log4j.Logger;
- public class FileBasedProperties implements Properties {
- private static final Logger LOGGER = Logger.getLogger(FileBasedProperties.class);
- private ResourceBundle properties;
- private String filename;
- public FileBasedProperties() {
- }
-
- public FileBasedProperties(String basename) {
- this.filename = basename;
- properties = PropertyResourceBundle.getBundle(basename);
- }
- public String get(String key) {
- return properties.getString(key);
- }
- public String get(String key, String defaultValue) {
- try {
- String property = properties.getString(key);
- if (property != null) {
- return property;
- }
- } catch (RuntimeException e) {
- LOGGER.warn("Can't load " + key + " from the property file <" + filename + ">, use the default value: " + defaultValue);
- }
- return defaultValue;
- }
- }