/**
* 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. C++ STL之迭代器注意事项

    1.两个迭代器组成的区间是前闭后开的 2.如果迭代器的有效性,如果迭代器所指向的元素已经被删除,那么迭代器会失效 http://blog.csdn.net/hsujouchen/article/det ...

  2. JS对象基础

    JavaScript 对象 JavaScript 提供多个内建对象,比如 String.Date.Array 等等. 对象只是带有属性和方法的特殊数据类型. 访问对象的属性 属性是与对象相关的值. 访 ...

  3. 【转载】git命令和svn的对比

    首先,要明确的是,git和svn是完全不同的两种管理方式.他们的命令不是完全对等的. 下面只是一些相似方法的参考,而已. 参考 http://blog.csdn.net/chen198746/arti ...

  4. Android wakelock机制

      Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠,可以被用户态程序和内核获得. 这个锁可以是有超时的或者是没有超时的,超时的锁会在时间过去以后自动解锁. 如果没有锁了或者 ...

  5. JAVA中获取工程路径的方法

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  6. c#(.net) 导出 word表格

    做了差不多一周的导出Word,现在把代码贴出来   : ExportWord.cs using System; using System.Collections.Generic; using Syst ...

  7. 【jQuery】总结:筛选器、控制隐藏、操作元素style属性

    筛选器 -> http://blog.csdn.net/lijinwei112/article/details/6938134 常用到的: $("tr[id=ac_"+id+ ...

  8. 【转】使用autolayout常见错误

    原文网址:http://www.cnblogs.com/xiaokanfengyu/p/4175091.html 使用autolayout常见错误 1:The view hierarchy is no ...

  9. TCP/IP详解学习笔记(8)-DNS域名系统

    前面已经提到了访问一台机器要靠IP地址和MAC地址,其中,MAC地址可以通过ARP协议得到,所以这对用户是透明的,但是IP地址就不行,无论如何用户都需要用一个指定的IP来访问一台计算机,而IP地址又非 ...

  10. 不重启mysql情况修改参数变量

    地球人都知道,更新mysql配置my.cnf需要重启mysql才能生效,但是有些时候mysql在线上,不一定允许你重启,这时候应该怎么办呢? 看一个例子: 1 2 3 4 5 6 7 8 9 10 m ...