不多说,直接上代码
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 具有按文件顺序加载功能。
*
* @author shooter
*
*/
public class OrderProperties {
private static final long serialVersionUID = 8380510196573353417L;
private List<String> keys = new ArrayList<String>(); // 键,通过此List实现属性文件顺序读取
private Map<String, String> valueMap = new HashMap<String, String>(); // 存储属性文件值
/**
* 通过键获取值
*
* @param key
* @return
*/
public String getProperty(String key) {
return valueMap.get(key);
}
// for example, java regex
public List<String> getKeys(String keyPattern) {
Pattern pat = Pattern.compile(keyPattern);
List<String> kl = new ArrayList<String>();
for (String k : keys) {
if (pat.matcher(k).matches()) {
kl.add(k);
}
}
return kl;
}
/**
* 添加元素
*
* @param fileName
* @param line
* @throws IOException
*/
public synchronized void put(String fileName, String line)
throws IOException {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write("\n" + line);
writer.flush();
writer.close();
}
/**
* 修改元素
*
* @param fileName
* @param key
* @param value
* @throws IOException
*/
public synchronized void update(String fileName, String[] key,
String[] value) throws IOException {
try {
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
byte[] buff = new byte[(int) bin.available()];
bin.read(buff);
FileOutputStream fout = new FileOutputStream(fileName);
String str = new String(buff);
String[] lines = str.split("\n");
int j = 0;
for (String line : lines) {
for (int i = 0; i < key.length; i++) {
if (key[i].equals(line.split("=")[0])) {
line = value[i];
break;
}
}
String newLine = "\n";
if (j == (lines.length - 1)) {
newLine = "";
}
fout.write((line + newLine).getBytes());
j++;
}
fout.flush();
fout.close();
bin.close();
} catch (IOException e) {
}
}
/**
* 加载,获得所有
*
* @param fileName
* @throws IOException
*/
public synchronized void load(String fileName) throws IOException {
BufferedReader bread = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bread.readLine()) != null) {
line = line.trim();
if (line.equals(""))
continue;
if (line.endsWith("\\"))
line = line.substring(0, line.length() - 1); // compatible to
// properties
// file: end \
if (line.indexOf('=') != -1) {
lines.add(line);
} else {
String s = lines.get(lines.size() - 1);
s += line;
lines.set(lines.size() - 1, s);
}
}
// parse key-value
for (String l : lines) {
String k = l.substring(0, l.indexOf("=")).trim();
String v = l.substring(l.indexOf("=") + 1).trim();
keys.add(k);
valueMap.put(k, v);
}
bread.close();
}
/**
* 获得所有key
*
* @return
*/
public List<String> getKeys() {
return keys;
}
/**
* 中文乱码时用
*
* @param dataStr
* @return
*/
public static String Unicode2GBK(String dataStr) {
int index = 0;
StringBuffer buffer = new StringBuffer();
while (index < dataStr.length()) {
if (!"\\u".equals(dataStr.substring(index, index + 2))) {
buffer.append(dataStr.charAt(index));
index++;
continue;
}
String charStr = "";
charStr = dataStr.substring(index + 2, index + 6);
char letter = (char) Integer.parseInt(charStr, 16);
try {
buffer.append(new String((letter + "").getBytes("gbk"), "gbk"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
index += 6;
}
return buffer.toString();
}
@Override
public String toString() {
return valueMap.toString();
}
public static void main(String[] args) {
OrderProperties prop = new OrderProperties();// 属性集合
String path = "D:/Test/webapp/WEB-INF/conf/filterMethodArgs.properties";
// 1.加载属性文件
try {
prop.load(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.添加元素
prop.put(path, "我是值");
// 3.修改元素
prop.update(path, new String[] { "我是键" }, new String[] { "我是值" });
}
}