您的位置:首页 > 运维架构

PropertiesHelper

2015-10-26 22:41 357 查看
最近遇到很多地方使用Properties文件作为配置文件,key-value的模式让解析传输都非常方便。接下来首先介绍一下Properties类,然后给出一些实现。

Properties

java.lang.Object
- java.util.Dictionary<K,V>
- java.util.Hashtable<Object,Object>
- java.util.Properties


Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。

注意点

因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。类似地,如果在“不安全”的 Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。

此类是线程安全的(因为继承了线程安全类HashTable):多个线程可以共享单个 Properties 对象而无需进行外部同步

Properties在遇到中文的时候会出现乱码:一般情况下中文会被重写成Unicode编码,如果出现乱码。有两种解决方案:使用native2ascii.exe转换,网上很多,自行查找;在读取的时候,强行转化成UTF-8码/article/4105696.html,我并没有亲自尝试,有兴趣的朋友可以试试

在将文件转化成流文件的时候,一般有两种方式,下方代码中都有实现:使用url实例化InputStream(in = new BufferedInputStream(new FileInputStream(path)););使用jar自带的getSystemResourceAsStream读取,值得注意的是非静态方法使用this.class.ClassLoader来获取类加载器

PropertiesHelper

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
* Properities辅助类
*
* @author Zhang_pc
*
*/
public class PropertiesHelper {
public static void main(String[] args) {
String path = "./src/config/mysql.properties";
printPros(path);
write2Properties(path, "port", "3307");
printPros(path);
}

/**
* 获取配置文件键值对
*/
public static Map<String, String> getProps(String path) {
Properties prop = new Properties();
Map<String, String> props = new HashMap<String, String>();
InputStream in = null;
try {
//这地方可以根据url来解析文件的,需要定位文件,不太好
//in = new BufferedInputStream(new FileInputStream(path));
in = ClassLoader.getSystemResourceAsStream("config/mysql.properties");
prop.load(in);
Enumeration<String> names = (Enumeration<String>) prop.propertyNames();

while (names.hasMoreElements()) {
String name = names.nextElement();
props.put(name, prop.getProperty(name));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return props;
}

/**
* 根据指定的key获取对应的配置值
*/
public static String getByKey(String path, String key) {
Map<String, String> map = getProps(path);
if (!map.isEmpty()) {
return map.get(key);
}
return null;
}

/**
* 向指定的文件写入key-val,并且写入注释
*/
public static void write2Properties(String path,String key,String val){
Properties prop = new Properties();

//读取配置文件
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(path));
prop.load(in);
prop.setProperty(key, val);
out = new FileOutputStream(path);
prop.store(out, "Update "+key);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/**
* 打印
* @param path
*/
public static void printPros(String path){
Map<String, String> map = getProps(path);
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + " : " + map.get(key));
}
}
}


结束语:上述代码可以简单的操作Properties文件,直接复制在eclipse上就可以运行观察。

完结

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: