PropertiesUtils 在网上找到一个小工具
2013-09-22 15:34
253 查看
package bs.util.io; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import java.util.Set; /** * 读取属性文件属性. * * @author baishui2004 * @version 1.0 */ public class PropertiesUtils { /** * 获取.properties属性文件所有属性. * * @param path * 属性文件路径 * @return <code>Properties</code> 属性 * @throws IOException * IO Exception */ public static Properties getProperties(String path) throws IOException { Properties properties = new Properties(); InputStream in = null; try { in = new FileInputStream(new File(path)); properties = getProperties(in); } finally { if (in != null) { in.close(); } } return properties; } /** * 获取所有属性. * * @param in * InputStream * @return <code>Properties</code> 属性 * @throws IOException * IO Exception */ public static Properties getProperties(InputStream in) throws IOException { Properties properties = new Properties(); properties.load(in); return properties; } /** * 获取.properties属性文件所有属性的Map集合. * * @param path * 属性文件路径 * @return <code>Map<String, String></code> 属性的Map集合 * @throws IOException * IO Exception */ public static Map<String, String> getPropertiesMap(String path) throws IOException { Properties properties = getProperties(path); return getPropertiesMap(properties); } /** * 获取所有属性. * * @param in * InputStream * @return <code>Map<String, String></code> 属性的Map集合 * @throws IOException * IO Exception */ public static Map<String, String> getPropertiesMap(InputStream in) throws IOException { Properties properties = getProperties(in); return getPropertiesMap(properties); } /** * 获取properties属性的Map集合. * * @param properties * 属性 * @return <code>Map<String, String></code> 属性的Map集合 */ public static Map<String, String> getPropertiesMap(Properties properties) { Set<String> keySets = properties.stringPropertyNames(); Object[] keys = keySets.toArray(); Map<String, String> map = new LinkedHashMap<String, String>(); for (int i = 0; i < keys.length; i++) { String key = (String) keys[i]; map.put(key, properties.getProperty(key)); } return map; } /** * 获取.properties属性单个属性的方法,获取两个或两个以上属性值使用getPropertiesMap(path)方法. * * @param path * 属性文件路径 * @param property * 属性key * @return <code>String</code> 属性值 * @throws IOException * IO Exception * @see #getPropertiesMap(String) */ public static String getProperty(String path, String property) throws IOException { return getPropertiesMap(path).get(property); } }
package bs.util.io; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.IOException; |
import java.io.InputStream; |
import java.util.LinkedHashMap; |
import java.util.Map; |
import java.util.Properties; |
import java.util.Set; |
/** |
* 读取属性文件属性. |
* |
* @author baishui2004 |
* @version 1.0 |
*/ |
public class PropertiesUtils { |
/** |
* 获取.properties属性文件所有属性. |
* |
* @param path |
* 属性文件路径 |
* @return <code>Properties</code> 属性 |
* @throws IOException |
* IO Exception |
*/ |
public static Properties getProperties(String path) throws IOException { |
Properties properties = new Properties(); |
InputStream in = null; |
try { |
in = new FileInputStream(new File(path)); |
properties = getProperties(in); |
} finally { |
if (in != null) { |
in.close(); |
} |
} |
return properties; |
} |
/** |
* 获取所有属性. |
* |
* @param in |
* InputStream |
* @return <code>Properties</code> 属性 |
* @throws IOException |
* IO Exception |
*/ |
public static Properties getProperties(InputStream in) throws IOException { |
Properties properties = new Properties(); |
properties.load(in); |
return properties; |
} |
/** |
* 获取.properties属性文件所有属性的Map集合. |
* |
* @param path |
* 属性文件路径 |
* @return <code>Map<String, String></code> 属性的Map集合 |
* @throws IOException |
* IO Exception |
*/ |
public static Map<String, String> getPropertiesMap(String path) throws IOException { |
Properties properties = getProperties(path); |
return getPropertiesMap(properties); |
} |
/** |
* 获取所有属性. |
* |
* @param in |
* InputStream |
* @return <code>Map<String, String></code> 属性的Map集合 |
* @throws IOException |
* IO Exception |
*/ |
public static Map<String, String> getPropertiesMap(InputStream in) throws IOException { |
Properties properties = getProperties(in); |
return getPropertiesMap(properties); |
} |
/** |
* 获取properties属性的Map集合. |
* |
* @param properties |
* 属性 |
* @return <code>Map<String, String></code> 属性的Map集合 |
*/ |
public static Map<String, String> getPropertiesMap(Properties properties) { |
Set<String> keySets = properties.stringPropertyNames(); |
Object[] keys = keySets.toArray(); |
Map<String, String> map = new LinkedHashMap<String, String>(); |
for (int i = 0; i < keys.length; i++) { |
String key = (String) keys[i]; |
map.put(key, properties.getProperty(key)); |
} |
return map; |
} |
/** |
* 获取.properties属性单个属性的方法,获取两个或两个以上属性值使用getPropertiesMap(path)方法. |
* |
* @param path |
* 属性文件路径 |
* @param property |
* 属性key |
* @return <code>String</code> 属性值 |
* @throws IOException |
* IO Exception |
* @see #getPropertiesMap(String) |
*/ |
public static String getProperty(String path, String property) throws IOException { |
return getPropertiesMap(path).get(property); |
} |
} |
相关文章推荐
- 网上找到的一个SQLServer取随机数和随机日期的方法
- 一个网上找到的,在Grid中嵌套Grid的示例:Nested Grids Example
- 一个好工具-everything-可以找到浏览器的所有缓存
- 在网上找到的一个表单验证类,用起来省事省力
- 金额转大写的函数 (网上找到的几个有Bug 只好自己写了一个)
- 一个网上找到的,在Grid中嵌套Grid的示例:Nested Grids Example
- 从网上找到的一个quartz的一个例子
- 网上找到一个星际的光标库,刚装上,一个字,酷!
- ExtJs中文文档下载,今天在网上找到了ExtJs的中文翻译的,我把它编译成了一个电子书,在这里共享给大家
- 网上找到一个可以用来测试网站安全性能的测试ASP页面
- 在网上找到的一个解决mysql命令窗口一闪而过的有效方法
- 从网上找到一个清晰CSS视频教程和大家分享一下
- 今天在网上找QQ头像,找到两个字,太强了,一个我老婆的名字,另一个.....用了肯定有人打我脸
- wap获取手机号码,今天uc开启云加速,用公司的工具类获取是null,但是另外一个项目可以,应该这个项目的工具旧少了一些代码。。,网上找了 方法自己加可以
- 分享网上找到的一个中国象棋源码
- 在一个博友的博客上找到的希望以后可以用到~程序员提高效率的十个工具
- Winform编程总结1—从网上找到一个winform控件,实现打开word的功能
- 如何快速找到一个新工具在linux里面
- 在网上找到一个有用的macro
- 对live555封装的比较好的一个类,网上找到的,觉得不错,给大家共享