铂西学习日记---Properties工具类

假如有一些固定的属性像版本号,ip地址号,我想修改其中的属性,但是项目一旦发布后就得从第一步开始修改,每次都得经历编写-->打包-->发布的过程,很麻烦,配置文件很好的解决这个问题。

package io_12_properties;

import java.io.*;
import java.util.Properties;

/**
 * ResourceBundle只可读,不可写
 * Properties 可读可写
 */
public class demo {
    public static String version = "";
    public static String name = "";
    public static String pwd = "";
    //静态代码块,只执行一次,先执行
//    static {
//        readConfig();
//    }
    /**
     * 读取配置文件
     */
    public static void readConfig(){
        Properties properties = new Properties();
        try {
            //如果想将配置文件放到指定的文件中---通过当前线程的类加载器来加载指定包下的配置文件
//            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("io_12_properties/test.properties");
            InputStream inputStream = new FileInputStream("test.properties");
            properties.load(inputStream);
            //从Properties中获取数据
            version = properties.getProperty("app.version");
            name = properties.getProperty("db.name");
            pwd = properties.getProperty("db.pwd");
            //关闭流
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入配置文件
     */
    public static void writeConfig(String version ,String name, String pwd){
        Properties properties = new Properties();
        properties.put("app.version",version);
        properties.put("db.name",name);
        properties.put("db.pwd",pwd);

        //写入
        try {
            OutputStream outputStream = new FileOutputStream("test.properties");

            //写文件--第二个参数为描述
            properties.store(outputStream,"update value!");
            //关闭流
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除配置文件
     */
    public static void clear(){
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("test.properties");
            properties.load(inputStream);
            properties.clear();
            OutputStream outputStream = new FileOutputStream("test.properties");
            properties.store(outputStream,"");
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //测试
    public static void main(String[] args) {
        //写
//        writeConfig("2.0","lsq666","666666");
        //读
        readConfig();
        System.out.println(demo.version);
        System.out.println(demo.name);
        System.out.println(demo.pwd);
        clear();

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小狗铂西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值