Java代码操作properties文件(读取,新增/修改,删除)

Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类, xx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.properties。
以key=value 的 键值对的形式进行存储值。 key值不能重复。

项目结构

 data.properties文件内容

username=root
password=root

 properties的工具类

package properties;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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").substring(1,(path + "data.properties").length());
        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);
     */

}

测试上面工具类的使用 

public static void main(String[] args) {

       String password = PropertiesUtil.getValue("password");
        System.out.println(password);
        PropertiesUtil.setValue("password","123456");
        String password1 = PropertiesUtil.getValue("password");
        System.out.println(password1);
    }

第一次执行结果
root
123456

第二次执行结果

123456
123456

第一次运行结果分析:

因为data.properties 自动编译后他的源文件password就是root,所以输出root ,然后PropertiesUtil工具类修改了编译后的文件把password改成了123456,所以输出的是123456

第二次结果分析(重新运行,不动代码的情况下)

因为在idea或者别的开发工具中,没有修改文件,他执行的还是上次编译后的文件,因为第一次最后的编译后的文件已经修改,所以两次执行的都是123456

部署项目到服务器上,项目是除非更新,重新编译,否则读取的Properties 文件一直都是被更改的

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值