properties 文件读取和写入

读取properties 文件

通过上述的 classloader 获取项目根路径 ,我们来加载 properties 文件

user=hjy
passwd=123456

image-20211214164815599

public class App
{
    public static void main( String[] args ) throws ClassNotFoundException, IOException {
        // 随便 new 的一个对象 ,用来获取 classloader
        hello hello = new hello();
        // 相当于获取路径简化一步 ,直接获得输入流
        InputStream resource = hello.getClass().getClassLoader().getResourceAsStream("test.properties");
        Properties properties = new Properties();
      	// 加载输入流 ,这样我们就可以获取其中的键值对了 
        properties.load(resource);
        System.out.println(properties.getProperty("user"));
        System.out.println(properties.getProperty("passwd"));
    }
}

当然 ,我们也可以使用原始的方法

public class App
{
    public static void main( String[] args ) throws ClassNotFoundException, IOException {
        // 随便 new 的一个对象 ,用来获取 classloader
        hello hello = new hello();
      // 获取根路径
        String path = hello.getClass().getClassLoader().getResource("test.properties").getPath();
        FileInputStream resource = new FileInputStream(path);
        Properties properties = new Properties();
        properties.load(resource);
        System.out.println(properties.getProperty("user"));
        System.out.println(properties.getProperty("passwd"));
    }
}

设置properties 文件

首先 ,我们需要指定设置的值

properties.setProperty(key,value);

然后获取文件路径的 输入流 ,调用 以下代码 ,修改文件

properties.store(stream,null);
Properties properties = getProperties(location);
properties.setProperty(key,value);
FileOutputStream stream = null;
try {
  stream = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
  properties.store(stream,null);
} catch (IOException e) {
  e.printStackTrace();
}

工具类

我们可以写一个工具类 ,来实现 properties 文件的读取的设置

package com.hjy.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class MyProperties {
    /**
     * 获取 properties 对象
     * @param location
     * @return
     */
    public static Properties getProperties(String location) {
        Properties properties = new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(location));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 获取 properties 文件的属性值
     * @param location
     * @param key
     * @return
     */
    public static String getProperty(String location,String key) {
        Properties properties = getProperties(location);
        return properties.getProperty(key);
    }

    /**
     * 设置指定位置的 properties 文件的属性值
     * @param location
     * @param key
     * @param value
     */
    public static void setProperty(String location,String key ,String value) {
        Properties properties = getProperties(location);
        properties.setProperty(key,value);
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
            properties.store(stream,null);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

最后主函数中测试

    public static void main(String[] args) throws IOException {
        String name = MyProperties.getProperty("test.properties", "name");
        System.out.println(name);
      	// 设置的是打包后的 properties 文件 ,而不是项目中的这个
        MyProperties.setProperty("test.properties","name","hjy");
        String name1 = MyProperties.getProperty("test.properties", "name");
        System.out.println(name1);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值