java操做Properties文件

發現properties太大了,並且沒有排序,查找起來很麻煩,因而寫了這樣一個類。 html

固然也參考了許多網上的資料。。。 java

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,以及開源中國裏的一篇文章,地址我忘了,就不加上了。 code

相關文章
相關標籤/搜索