java file 封装的功能室_Java封装 properties文件操作

/*** PropKit. PropKit can load properties file from CLASSPATH or File object.*/

public classPropKit {private static Prop prop = null;private static final Map map = new ConcurrentHashMap();privatePropKit() {}/*** Using the properties file. It will loading the properties file if not loading.

*@see#use(String, String)*/

public staticProp use(String fileName) {returnuse(fileName, Const.DEFAULT_ENCODING);

}/*** Using the properties file. It will loading the properties file if not loading.

*

* Example:

* PropKit.use("config.txt", "UTF-8");

* PropKit.use("other_config.txt", "UTF-8");

* String userName = PropKit.get("userName");

* String password = PropKit.get("password");

*

* userName = PropKit.use("other_config.txt").get("userName");

* password = PropKit.use("other_config.txt").get("password");

*

* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");

*

*@paramfileName the properties file's name in classpath or the sub directory of classpath

*@paramencoding the encoding*/

public staticProp use(String fileName, String encoding) {

Prop result=map.get(fileName);if (result == null) {

result= newProp(fileName, encoding);

map.put(fileName, result);if (PropKit.prop == null)

PropKit.prop=result;

}returnresult;

}/*** Using the properties file bye File object. It will loading the properties file if not loading.

*@see#use(File, String)*/

public staticProp use(File file) {returnuse(file, Const.DEFAULT_ENCODING);

}/*** Using the properties file bye File object. It will loading the properties file if not loading.

*

* Example:

* PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");

* Strig userName = PropKit.use("my_config.txt").get("userName");

*

*@paramfile the properties File object

*@paramencoding the encoding*/

public staticProp use(File file, String encoding) {

Prop result=map.get(file.getName());if (result == null) {

result= newProp(file, encoding);

map.put(file.getName(), result);if (PropKit.prop == null)

PropKit.prop=result;

}returnresult;

}public staticProp useless(String fileName) {

Prop previous=map.remove(fileName);if (PropKit.prop ==previous)

PropKit.prop= null;returnprevious;

}public static voidclear() {

prop= null;

map.clear();

}public staticProp getProp() {if (prop == null)throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");returnprop;

}public staticProp getProp(String fileName) {returnmap.get(fileName);

}public staticString get(String key) {returngetProp().get(key);

}public staticString get(String key, String defaultValue) {returngetProp().get(key, defaultValue);

}public staticInteger getInt(String key) {returngetProp().getInt(key);

}public staticInteger getInt(String key, Integer defaultValue) {returngetProp().getInt(key, defaultValue);

}public staticLong getLong(String key) {returngetProp().getLong(key);

}public staticLong getLong(String key, Long defaultValue) {returngetProp().getLong(key, defaultValue);

}public staticBoolean getBoolean(String key) {returngetProp().getBoolean(key);

}public staticBoolean getBoolean(String key, Boolean defaultValue) {returngetProp().getBoolean(key, defaultValue);

}public static booleancontainsKey(String key) {returngetProp().containsKey(key);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值