idea如何读取配置properties文件以及加载外部文件目录下的配置

如何去读properties结尾的文件,方便程序的灵活配置参数
首先建立`PropertiesReader类一般放在util包下面
public class PropertiesReader {

/**
 * 读取配置文件
 */
private  Properties getProperties(){
    Properties p = null;
    try{
        // 读取url配置文件configuration.properties为具体配置文件的名字
      InputStream in = this.getClass().getClassLoader().getResourceAsStream("configuration.properties");
        p = new Properties();
        p.load(in);
    }catch (Exception e){
        System.out.println("====读取异常====="+e.getMessage());
        p = null;
    }
    return p;
}

/**
 * 查询配置文件中指定参数对应的值
 */
public static String getProperty(String propertyName){
    PropertiesReader reader = new PropertiesReader();
    Properties p =  reader.getProperties();
    if(null == p)
        return null;
    String result = p.getProperty(propertyName);
    return result;
}

configuration.properties具体如下
在这里插入图片描述
项目引入参数

props.put("bootstrap.servers",PropertiesReader.getProperty("product_IP"));
PropertiesReader.getProperty("product_IP")
即可灵活引入参数,方便配置

加载外部文件目录下的配置
添加filepath

  private  static Logger logger = Logger.getLogger("FilePath");
    //读取文件路径 在项目文件的上一次conf文件夹里面
    public static String getConfFileDir(){
        String path = System.getProperty("user.dir")+File.separator+".."+File.separator + "conf";
        return path;
    }
    public static String getConfFile(String fileNamePath){
        String path = getConfFileDir() + File.separator + fileNamePath;
        judeFileExists(new File(path));
        return path;
    }
    // 判断文件是否存在
    public static void judeFileExists(File file) {
        if (file.exists()) {
            return;
        } else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

在ConstantUti修改

 //    配置文件名称
    public static final String PROPERTIES_PATH = FilePath.getConfFile("外部文件目录");

PropertiesReader

 private  Properties getProperties(){
        Properties pps = null;
        try{
            // 读取url配置文件
            InputStream in = new FileInputStream(ConstantUtil.PROPERTIES_PATH);
            pps = new Properties();
            pps.load(in);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("===========读取配置文件异常========="+e.getMessage());
            pps = null;
        }
        return pps;
    }


/**
     * 查询配置文件中指定参数对应的值
     * @param propertyName 参数名称
     */

    public static String getProperty(String propertyName){
        PropertiesReader reader = new PropertiesReader();
        Properties pps =  reader.getProperties();
        if(null == pps)
            return null;
        String result = pps.getProperty(propertyName);
        return result;
    }

其他一切照旧即可

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IDEA配置properties文件有以下几个步骤: 1. 创建一个Properties类的实例,并设置需要的键值对。可以使用setProperty()方法来设置属性的键和值。 2. 使用FileWriter类将Properties对象中的属性存储到文件中。可以使用store()方法将属性写入到文件中,需要提供一个FileWriter对象和一个注释。 3. 使用FileReader类从文件读取Properties对象的属性。可以使用load()方法从文件加载属性,需要提供一个FileReader对象。 4. 在IDEA配置properties文件,可以按照以下步骤进行操作: - 在项目中创建一个properties文件,可以选择Resource Bundle类型的文件。 - 将配置信息以文本文件的格式写入到properties文件中。 综上所述,通过以上方法可以在IDEA配置properties文件。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Properties类简介 及 IDEA properties文件配置方法](https://blog.csdn.net/weixin_40901329/article/details/79845282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [IDEA使用properties配置文件进行mysql数据库连接的教程图解](https://download.csdn.net/download/weixin_38682406/12755617)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值