java 实时读取、修改properties文件

来源:互联网 发布:俾斯麦模型淘宝 编辑:程序博客网 时间:2024/05/18 02:52
public class Config {static String savePath = Config.class.getResource("/config.properties").getPath();  // 根据key读取valuepublic static String getValue(String key) {try {//getResourceAsStream 有缓存  修改后 还是返回以前的。。//InputStream in = Config.class.getClassLoader().getResourceAsStream("config.properties");Properties config = new Properties();try {InputStream in =new BufferedInputStream(new FileInputStream(savePath)); config.load(in);in.close();} catch (IOException e) {System.out.println("加载初始化配置文件失败!");}String value = config.getProperty(key);return value;} catch (Exception e) {System.err.println("读取初始化配置文件失败!");e.printStackTrace();return null;}}    /**     * 修改文件中的值     * @param Key 要修改的 key 值     * @param value 要修改的 key 值对应的 value 值     */    public static void setProper(String Key, Object value) {        Properties pro = new Properties();        FileInputStream fis = null;        String path = Config.class.getClassLoader().getResource("config.properties").getFile();        // 路径中有空格,路径会将空格自动转为 %20,所以把他替换为了空格        path = path.replace("%20", " ");        File file = new File(path);        BufferedInputStream bis = null;        try {            fis = new FileInputStream(file);            bis = new BufferedInputStream(fis);            pro.load(bis);            FileOutputStream fos = new FileOutputStream(file);            pro.setProperty(Key, String.valueOf(value));            pro.store(fos, null);            fos.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

0 0
原创粉丝点击