zoukankan      html  css  js  c++  java
  • Java代码操作properties文件(读取,新增/修改,删除)

    项目中需要用到操作properties文件中的数据,记录一下

    package com.bonc.savepic.save;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    /**
     * @Author clj
     * @SinceDate 2019/3/26 17:05
     * @Description
     */
    public class PropertiesUtil {
    
        /**
         * 获取Properties对象
         * @return
         */
        public static Properties getProperties(){
            Properties properties = new Properties();
            InputStream inputStream = null;
            try {
                //data.properties在resources目录下
                inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("data.properties");
                properties.load(inputStream);
            } catch (FileNotFoundException e) {
                System.out.println("data.properties文件未找到!");
            } catch (IOException e) {
                System.out.println("出现IOException");
            } finally {
                try {
                    if (null != inputStream){
                        inputStream.close();
                    }
                } catch (IOException e) {
                    System.out.println("data.properties文件流关闭出现异常");
                }
            }
            return properties;
        }
    
        /**
         * 根据key查询value值
         * @param key key
         * @return
         */
        public static String getValue(String key){
            Properties properties = getProperties();
            String value = properties.getProperty(key);
            return value;
        }
    
        /**
         * 新增/修改数据
         * @param key 
         * @param value
         */
        public static void setValue(String key, String value){
            Properties properties = getProperties();
            properties.setProperty(key, value);
            //此处获取的路径是target下classes
    
            //这里的path是项目文件的绝对路径
            //先获取项目绝对路径:Thread.currentThread().getContextClassLoader().getResource("").getPath();
            //然后在项目路径后面拼接"properties/sysConfig.properties";
            // 原注释
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            path = path + "data.properties";
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(path);
                properties.store(fileOutputStream, "注释");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != fileOutputStream){
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    System.out.println("data.properties文件流关闭出现异常");
                }
            }
        }
    
        /**
         * 删除和修改只有语句不同
         * properties.remove(key);
         */
    }
  • 相关阅读:
    《构建之法》第四章 第十七章阅读笔记
    2016012037+小学四则运算练习软件项目报告
    阅读《构建之法》所得与初步思考
    随机产生300个四则运算题
    我与软件工程的相识,相知
    转 如何快速看懂一个大型程序 http://blog.csdn.net/jysg9/article/details/24193181
    SQL Server 存储过程(转)
    sql 视图的作用(转http://www.cup.edu.cn/nic/Programing/database/34943.htm)
    linux性能问题(CPU,内存,磁盘I/O,网络)( 转载)
    qtp测试计划
  • 原文地址:https://www.cnblogs.com/cailijuan/p/10601480.html
Copyright © 2011-2022 走看看