发现properties太大了,而且没有排序,查找起来很麻烦,于是写了这样一个类。
当然也参考了许多网上的资料。。。
public class OperatorProperties {
public static void print(String filePath){
OrderedProperties prop = achieveProperties(filePath);
Map orderedProp = new TreeMap(prop);
Iterator itr = orderedProp.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry) itr.next();
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
private static File achieveFile(String filePath){
File file = new File(filePath);
if(!file.exists()){
try {
file.createNewFile();
System.out.println("文件"+filePath+"不存在,创建新文件");
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("成功获取文件"+filePath);
}
return file;
}
public static void saveOrUpdate(String filePath,String paraName,String paraValue){
OrderedProperties prop = achieveProperties(filePath);
try {
OutputStream os = new FileOutputStream(filePath);
prop.setProperty(paraName, paraValue);
prop.store(os, "Hello World");
System.out.println("已保存数据到"+filePath);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static OrderedProperties achieveProperties(String filePath){
File file = achieveFile(filePath);
OrderedProperties prop =OrderedProperties.getPropertiesInstance();
try {
InputStream is = new FileInputStream(file);
prop.load(is);
System.out.println("已获取"+filePath+"文件");
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
public static void copy(String baseFilePath,String desFilePath){
OrderedProperties baseProp = achieveProperties(baseFilePath);
OrderedProperties desProp = achieveProperties(desFilePath);
try {
OutputStream os = new FileOutputStream(desFilePath);
Map orderedProp = new TreeMap(baseProp);
Iterator itr = orderedProp.entrySet().iterator();
int number = 0;
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry) itr.next();
desProp.setProperty((String)entry.getKey(), (String)entry.getValue());
System.out.println(entry.getKey() + "=" + entry.getValue());
number++;
}
System.out.println("共拷贝"+number+"条数据");
desProp.store(os, "Hello World");
System.out.println("从"+baseFilePath+"到"+desFilePath+"的数据复制完毕");
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void print(String filePath,String key){
OrderedProperties prop = achieveProperties(filePath);
Map orderedProp = new TreeMap(prop);
if(orderedProp.containsKey(key)){
System.out.println(key + "=" + orderedProp.get(key));
}else{
System.out.println("key为"+key+"的值不存在");
}
}
public static void delete(String filePath,String key){
OrderedProperties prop = achieveProperties(filePath);
try {
OutputStream os = new FileOutputStream(filePath);
prop.remove(key);
prop.store(os, "Hello World");
System.out.println("已保存数据到"+filePath);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}
class OrderedProperties extends Properties{
private static final OrderedProperties properties = new OrderedProperties();
private OrderedProperties(){
super();
}
public static final OrderedProperties getPropertiesInstance(){
return properties;
}
public synchronized Enumeration keys() {
Enumeration keysEnum = super.keys();
Vector keyList = new Vector();
while(keysEnum.hasMoreElements()){
keyList.add(keysEnum.nextElement());
}
Collections.sort(keyList);
return keyList.elements();
}
}
参考资料:http://www.rgagnon.com/javadetails/java-0614.html,以及开源中国里的一篇文章,地址我忘了,就不加上了。