首页

通过初始化工具类InitConfUtils对properties配置文件实现获取及更新键值等操作

标签:工具类,Properties,配置文件,常用,初始化     发布时间:2017-03-22   

通过PropertiesHandle类实现配置文件的读取、新增或更新键值,更改后数据存入指定的文件,具体参见代码

import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.util.Properties;@b@@b@public class PropertiesHandle {@b@@b@    private Properties propertie;@b@    private InputStream inputFile;@b@    private OutputStream outputFile;@b@    @b@    /**@b@     * 初始化PropertiesHandle类@b@     */@b@    public PropertiesHandle()@b@    {@b@        propertie = new Properties();@b@    }@b@    @b@    /**@b@     * 初始化PropertiesHandle类@b@     * @param filePath 要读取的配置文件的路径+名称@b@     */@b@    public PropertiesHandle(String filePath)@b@    {@b@        propertie = new Properties();@b@        try {@b@        	if(filePath.indexOf(":") != -1){@b@        		inputFile = new FileInputStream(filePath);@b@        	}else{@b@        		inputFile = getClass().getResourceAsStream(filePath);@b@        	}@b@        	propertie.load(inputFile);@b@            inputFile.close();@b@        } catch (FileNotFoundException ex) {@b@            ex = new FileNotFoundException("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");@b@            ex.printStackTrace();@b@        } catch (IOException ex) {@b@            ex = new IOException("装载文件--->失败!");@b@            ex.printStackTrace();@b@        } catch (Exception ex) {@b@            ex.printStackTrace();@b@        }@b@    }@b@    @b@    /**@b@     * 初始化PropertiesHandle类@b@     * @param is 要读取的配置文件流@b@     */@b@    public PropertiesHandle(InputStream is)@b@    {@b@        propertie = new Properties();@b@        try {@b@        	propertie.load(is);@b@            is.close();@b@        } catch (FileNotFoundException ex) {@b@            ex = new FileNotFoundException("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");@b@            ex.printStackTrace();@b@        } catch (IOException ex) {@b@            ex = new IOException("装载文件--->失败!");@b@            ex.printStackTrace();@b@        } catch (Exception ex) {@b@            ex.printStackTrace();@b@        }@b@    }@b@    @b@    /**@b@     * 重载函数,得到key的值@b@     * @param key 取得其值的键@b@     * @return key的值@b@     */@b@    public String getValue(String key)@b@    {@b@        if(propertie.containsKey(key)){@b@            String value = propertie.getProperty(key);//得到某一属性的值@b@            return value;@b@        }@b@        else @b@            return "";@b@    }@b@    @b@    /**@b@     * 重载函数,得到key的值@b@     * @param fileName properties文件的路径+文件名@b@     * @param key 取得其值的键@b@     * @return key的值@b@     */@b@    public String getValue(String filePath, String key)@b@    {@b@        try {@b@            String value = "";@b@            if(filePath.indexOf(":") != -1){@b@        		inputFile = new FileInputStream(filePath);@b@        	}else{@b@        		inputFile = getClass().getResourceAsStream(filePath);@b@        	}@b@            propertie.load(inputFile);@b@            inputFile.close();@b@            if(propertie.containsKey(key)){@b@                value = propertie.getProperty(key);@b@                return value;@b@            }else@b@                return value;@b@        } catch (FileNotFoundException e) {@b@            e.printStackTrace();@b@            return "";@b@        } catch (IOException e) {@b@            e.printStackTrace();@b@            return "";@b@        } catch (Exception ex) {@b@            ex.printStackTrace();@b@            return "";@b@        }@b@    }@b@    @b@    /**@b@     * 清除properties文件中所有的key和其值@b@     */@b@    public void clear()@b@    {@b@        propertie.clear();@b@    }@b@    @b@    /**@b@     * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,@b@     * 当key不存在时,该key的值是value@b@     * @param key 要存入的键@b@     * @param value 要存入的值@b@     */@b@    public void setValue(String key, String value)@b@    {@b@        propertie.setProperty(key, value);@b@    }@b@    @b@    /**@b@     * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。@b@     * @param fileName 文件路径+文件名称@b@     * @param description 对该文件的描述@b@     */@b@    public void saveFile(String fileName, String description)@b@    {@b@        try {@b@            outputFile = new FileOutputStream(fileName);@b@            propertie.store(outputFile, description);@b@            outputFile.close();@b@        } catch (FileNotFoundException e) {@b@            e.printStackTrace();@b@        } catch (IOException ioe){@b@            ioe.printStackTrace();@b@        }@b@    }@b@}

通过InitConfUtils再对PropertiesHandle进行依赖使用,参见代码如下

import java.util.Map;@b@import java.util.concurrent.ConcurrentHashMap;@b@@b@public class InitConfUtils {@b@	@b@	static PropertiesHandle handle = new PropertiesHandle("/init.properties");@b@	private InitConfUtils(){@b@		//禁止实例化@b@	}@b@	private static Map<String, PropertiesHandle> rt_handle = new ConcurrentHashMap<String, PropertiesHandle>();@b@	@b@	public static String getParamValue(String paramName)@b@	{@b@		String value = handle.getValue(paramName);@b@		return value;@b@	}@b@	@b@	public static String getParamValue(String paramName,String defaultValue)@b@	{@b@		String value = handle.getValue(paramName);@b@		return "".equals(value)?defaultValue:value;@b@	}@b@	@b@	public static String getParamValueFromPath(String paramName, String name){@b@		PropertiesHandle handle = rt_handle.get(name);@b@		if(handle == null) handle = registe(name);@b@		return handle.getValue(paramName);@b@	}@b@	@b@	public synchronized static PropertiesHandle registe(String name){@b@		PropertiesHandle handle = rt_handle.get(name);@b@		if(handle == null){@b@			handle = new PropertiesHandle("/" + name);@b@			rt_handle.put(name, handle);@b@		}@b@		return handle;@b@	}@b@	@b@}