java操作Properties文件

本文介绍了一个改进的类实现,用于处理大型属性文件。通过引入有序属性类,实现了属性的按顺序打印、读取、更新、复制及查询功能,并提供了删除属性的方法。该实现有助于提高属性管理效率,简化查找过程。

发现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,以及开源中国里的一篇文章,地址我忘了,就不加上了。


转载于:https://my.oschina.net/u/241670/blog/86213

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值