/**
* Prop. Prop can load properties file from CLASSPATH or File object.
*/
public class Prop { private Properties properties = null; /**
* Prop constructor.
* @see #Prop(String, String)
*/
public Prop(String fileName) {
this(fileName, Const.DEFAULT_ENCODING);
} /**
* Prop constructor
* <p>
* Example:<br>
* Prop prop = new Prop("my_config.txt", "UTF-8");<br>
* String userName = prop.get("userName");<br><br>
*
* prop = new Prop("com/jfinal/file_in_sub_path_of_classpath.txt", "UTF-8");<br>
* String value = prop.get("key");
*
* @param fileName the properties file's name in classpath or the sub directory of classpath
* @param encoding the encoding
*/
public Prop(String fileName, String encoding) {
InputStream inputStream = null;
try {
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
if (inputStream == null)
throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
properties = new Properties();
properties.load(new InputStreamReader(inputStream, encoding));
} catch (IOException e) {
throw new RuntimeException("Error loading properties file.", e);
}
finally {
if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
}
} /**
* Prop constructor.
* @see #Prop(File, String)
*/
public Prop(File file) {
this(file, Const.DEFAULT_ENCODING);
} /**
* Prop constructor
* <p>
* Example:<br>
* Prop prop = new Prop(new File("/var/config/my_config.txt"), "UTF-8");<br>
* String userName = prop.get("userName");
*
* @param file the properties File object
* @param encoding the encoding
*/
public Prop(File file, String encoding) {
if (file == null)
throw new IllegalArgumentException("File can not be null.");
if (file.isFile() == false)
throw new IllegalArgumentException("Not a file : " + file.getName()); InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
properties = new Properties();
properties.load(new InputStreamReader(inputStream, encoding));
} catch (IOException e) {
throw new RuntimeException("Error loading properties file.", e);
}
finally {
if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
}
} public String get(String key) {
return properties.getProperty(key);
} public String get(String key, String defaultValue) {
String value = get(key);
return (value != null) ? value : defaultValue;
} public Integer getInt(String key) {
String value = get(key);
return (value != null) ? Integer.parseInt(value) : null;
} public Integer getInt(String key, Integer defaultValue) {
String value = get(key);
return (value != null) ? Integer.parseInt(value) : defaultValue;
} public Long getLong(String key) {
String value = get(key);
return (value != null) ? Long.parseLong(value) : null;
} public Long getLong(String key, Long defaultValue) {
String value = get(key);
return (value != null) ? Long.parseLong(value) : defaultValue;
} public Boolean getBoolean(String key) {
String value = get(key);
return (value != null) ? Boolean.parseBoolean(value) : null;
} public Boolean getBoolean(String key, Boolean defaultValue) {
String value = get(key);
return (value != null) ? Boolean.parseBoolean(value) : defaultValue;
} public boolean containsKey(String key) {
return properties.containsKey(key);
} public Properties getProperties() {
return properties;
}
}
/**
* PropKit. PropKit can load properties file from CLASSPATH or File object.
*/
public class PropKit { private static Prop prop = null;
private static final Map<String, Prop> map = new ConcurrentHashMap<String, Prop>(); private PropKit() {} /**
* Using the properties file. It will loading the properties file if not loading.
* @see #use(String, String)
*/
public static Prop use(String fileName) {
return use(fileName, Const.DEFAULT_ENCODING);
} /**
* Using the properties file. It will loading the properties file if not loading.
* <p>
* Example:<br>
* PropKit.use("config.txt", "UTF-8");<br>
* PropKit.use("other_config.txt", "UTF-8");<br><br>
* String userName = PropKit.get("userName");<br>
* String password = PropKit.get("password");<br><br>
*
* userName = PropKit.use("other_config.txt").get("userName");<br>
* password = PropKit.use("other_config.txt").get("password");<br><br>
*
* PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
*
* @param fileName the properties file's name in classpath or the sub directory of classpath
* @param encoding the encoding
*/
public static Prop use(String fileName, String encoding) {
Prop result = map.get(fileName);
if (result == null) {
result = new Prop(fileName, encoding);
map.put(fileName, result);
if (PropKit.prop == null)
PropKit.prop = result;
}
return result;
} /**
* Using the properties file bye File object. It will loading the properties file if not loading.
* @see #use(File, String)
*/
public static Prop use(File file) {
return use(file, Const.DEFAULT_ENCODING);
} /**
* Using the properties file bye File object. It will loading the properties file if not loading.
* <p>
* Example:<br>
* PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");<br>
* Strig userName = PropKit.use("my_config.txt").get("userName");
*
* @param file the properties File object
* @param encoding the encoding
*/
public static Prop use(File file, String encoding) {
Prop result = map.get(file.getName());
if (result == null) {
result = new Prop(file, encoding);
map.put(file.getName(), result);
if (PropKit.prop == null)
PropKit.prop = result;
}
return result;
} public static Prop useless(String fileName) {
Prop previous = map.remove(fileName);
if (PropKit.prop == previous)
PropKit.prop = null;
return previous;
} public static void clear() {
prop = null;
map.clear();
} public static Prop getProp() {
if (prop == null)
throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");
return prop;
} public static Prop getProp(String fileName) {
return map.get(fileName);
} public static String get(String key) {
return getProp().get(key);
} public static String get(String key, String defaultValue) {
return getProp().get(key, defaultValue);
} public static Integer getInt(String key) {
return getProp().getInt(key);
} public static Integer getInt(String key, Integer defaultValue) {
return getProp().getInt(key, defaultValue);
} public static Long getLong(String key) {
return getProp().getLong(key);
} public static Long getLong(String key, Long defaultValue) {
return getProp().getLong(key, defaultValue);
} public static Boolean getBoolean(String key) {
return getProp().getBoolean(key);
} public static Boolean getBoolean(String key, Boolean defaultValue) {
return getProp().getBoolean(key, defaultValue);
} public static boolean containsKey(String key) {
return getProp().containsKey(key);
}
}

Java封装 properties文件操作的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  3. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  4. properties文件操作

    properties文件操作类 可以使用java.util.Properties读取.properties文件中的内容 import java.io.InputStream; import java. ...

  5. Java读properties文件中文乱码问题的解决方法

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

  6. java读properties文件 乱码

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

  7. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  9. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

随机推荐

  1. Hibernate4.2.2使用Annotation配置

    1.在hibernate官网下载hibernate-release-4.2.2.Final.zip并解压 2.新建一个java project工程(20130619_Hibernate4.2.2_An ...

  2. Codeforces Beta Round #2B(dp+数学)

    贡献了一列WA.. 数学很神奇啊 这个题的关键是怎么才能算尾0的个数 只能相乘 可以想一下所有一位数相乘 除0之外,只有2和5相乘才能得到0 当然那些本身带0的多位数 里面肯定含有多少尾0 就含有多少 ...

  3. POJ 1276 (多重背包) Cash Machine

    题意: 有n种纸币,已知每种纸币的面值和数量,求所能凑成的不超过cash的最大总面值. 分析: 这道题自己写了一下TLE了,好可耻.. 找了份比较简洁的代码抄过来了..poj1276 #include ...

  4. asp.net批量发布博客到各大博客平台

    新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...

  5. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  6. 事务报错 [Exception] 当前 TransactionScope 已完成

    捕获异常的时候 偶尔会碰到这个异常报告 导致原因 //正确代码 using (TransactionScope ts = new TransactionScope(TransactionScopeOp ...

  7. RTP封装h264

    网络抽象层单元类型 (NALU): NALU头由一个字节组成,它的语法如下: +---------------+      |0|1|2|3|4|5|6|7|      +-+-+-+-+-+-+-+ ...

  8. Android平台下实现录音及播放录音功能的简介

    录音及播放的方法如下: package com.example.audiorecord; import java.io.File; import java.io.IOException; import ...

  9. 一天一个Java基础——泛型

    这学期的新课——设计模式,由我仰慕已久的老师传授,可惜思维过快,第一节就被老师挑中上去敲代码,自此在心里烙下了阴影,都是Java基础欠下的债 这学期的新课——算法设计与分析,虽老师不爱与同学互动式的讲 ...

  10. printk 驱动调试

    驱动的调试,printk()添加调试信息 printk相当于printf的孪生姐妹,它们一个运行在用户态,另一个则在内核态. 需要包含<linux/device.h>或者<linux ...