properties配置文件操作工具类
package com.util;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
public class PropertiesUtil {
public static void main(String[] args) {
PropertiesUtil pu = new PropertiesUtil("superUser.properties", false);
pu.setProperties("systemdev", "zhangyongbo");
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 = "";
public PropertiesUtil(String fileName, boolean bool, HttpServletRequest request) {
if (!bool) {
if (request != null) {
filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/classes/" + fileName;
} else {
filePath = new File(PropertiesUtil.class.getResource("/").getPath()) + "/" + fileName;
}
} else {
filePath = fileName;
}
getProps(filePath);
}
public PropertiesUtil(String fileName, boolean bool) {
if (!bool) {
filePath = new File(PropertiesUtil.class.getResource("/").getPath()) + "/" + fileName;
} else {
filePath = fileName;
}
getProps(filePath);
}
public PropertiesUtil(String fileName, HttpServletRequest request) {
if (request != null) {
filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/classes/" + fileName;
} else {
filePath = new File(PropertiesUtil.class.getResource("/").getPath()) + "/" + fileName;
}
getProps(filePath);
}
private void getProps(String filePath) {
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();
}
}
public String readValue(String key) {
return props.getProperty(key);
}
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 ignored) {
ignored.printStackTrace();
}
}
}
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 ignored) {
ignored.printStackTrace();
}
props = p;
}
}
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;
}
private String getPath(Class<PropertiesUtil> aClass) {
String strResult;
if (System.getProperty("os.name").toLowerCase().contains("window")) {
strResult = aClass.getResource("/").toString().replace("file:/", "").replace("%20", " ");
} else {
strResult = aClass.getResource("/").toString().replace("file:", "").replace("%20", " ");
}
return strResult;
}
}