springboot java读取/获取properties中的值

1、读取resources下面的properties

//获取redis.properties中的内容
      Properties properties = PropertiesLoaderUtils.loadAllProperties("redis.properties");
        String host3 = properties.getProperty("host3");
        String port3 = properties.getProperty("port3");
        String auth3 = properties.getProperty("auth3");
        String db3 = properties.getProperty("db3");
        System.out.println(host3);
        System.out.println(port3);
        System.out.println(auth3);
        System.out.println(db3);

打印结果

2、读取自定义路径的properties文件

package com.example.springbootdemo;

import java.io.FileReader;
import java.util.Properties;

public class PropertiesTest {
    private static Properties properties = new Properties();

    public static void main(String[] args) throws Exception {
        properties.load(new FileReader("C:\\java\\1.properties"));
        System.out.println("---"+getValue("key"));
        System.out.println("---"+getValue("key1"));
    }

    public static String getValue(String key) {
        if (properties.containsKey(key)) {
            return properties.getProperty(key);
        }
        return "";
    }
}

打印结果 

 

posted @ 2021-08-03 10:08  难忘是想起  阅读(0)  评论(0)    收藏  举报  来源