package com.util;
import java.io.*;
import java.util.*;
/**
* 对properties配置文件进行增删改查操作
* <p>
* Created by 张勇波 on 2016/8/18.
*/
public class PropertiesUtil {
public static void main(String[] args) {
// PropertiesUtil pu = new PropertiesUtil("", false);
// File f = new File(pu.getClass().getResource("/").getPath());
// System.out.println(f);
PropertiesUtil pu = new PropertiesUtil("superUser2.properties", false);
// pu.setProperties("systemdev", "zhangyongbo");
// pu.delProperties("systemdev");
Map<String, String> map = pu.readAllProperties();
for (String s : map.keySet()) {
System.out.println(s + " " + map.get(s));
}
}
/**
* 配置文件对象
*/
private static Properties props = null;
/**
* 配置文件物理路径
*/
private String filePath = "";
/**
* 构造函数
* 如果配置文件不存在则创建一个空文件
*
* @param fileName 配置文件名称
* @param bool 是否绝对路径.true是,fileName为配置文件物理路径。否则取classes路径,fileName为配置文件名称。
*/
public PropertiesUtil(String fileName, boolean bool) {
if (!bool) {
filePath = new File(PropertiesUtil.class.getResource("/").getPath()) + "/" + fileName;
} else {
filePath = fileName;
}
props = new Properties();
try {
File myFile = new File(filePath);
if (!myFile.exists())
myFile.createNewFile();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
// 关闭资源
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据key值读取配置的值
*
* @param key key值
* @return key键对应的值
*/
public String readValue(String key) {
return props.getProperty(key);
}
/**
* 更新(插入)一对配置
*
* @param key key值
* @param value value值
*/
public void setProperties(String key, String value) {
if (key != null && value != null && key.length() > 0) {
try {
OutputStream fos = new FileOutputStream(filePath);
props.setProperty(key, value);
props.store(fos, "Update:'" + key + "'value");
} catch (IOException e) {
}
}
}
/**
* 删除一对配置
*
* @param key key值
*/
public void delProperties(String key) {
if (key != null && key.length() > 0) {
Map<String, String> map = readAllProperties();
Properties p = new Properties();
String val = "";
for (String s : map.keySet()) {
if (s.equals(key)) {
val = map.get(s);
continue;
}
p.setProperty(s, map.get(s));
}
try {
FileWriter writer = new FileWriter(filePath);
p.store(writer, "delete:'" + key + "'value'" + val + "'");
} catch (IOException e) {
}
props = p;
}
}
/**
* 读取properties的全部信息
*
* @return Map<String, String>
*/
public Map<String, String> readAllProperties() {
// 保存所有的键值
Map<String, String> map = new HashMap<String, String>();
Enumeration<?> en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
map.put(key, Property);
}
return map;
}
/**
* 得到某一个类的路径
*
* @param aClass 类
* @return 类物理地址
*/
private String getPath(Class<PropertiesUtil> aClass) {
String strResult = null;
if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {
strResult = aClass.getResource("/").toString().replace("file:/", "").replace("%20", " ");
} else {
strResult = aClass.getResource("/").toString().replace("file:", "").replace("%20", " ");
}
return strResult;
}
}
转载于:https://my.oschina.net/u/2303129/blog/902659