获取properties配置文件的值

需要jar包

工具类:

package qingxia.tang.utils;

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

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Demo {
    private static Properties pro=new Properties();
    static {
        Resource res=new ClassPathResource("upload.properties");//加载配置文件
        InputStream isp=null;
        try {
            isp=res.getInputStream();//获取输入流
            pro.load(isp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(isp!=null) {
                    isp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static String getValue(String key) {
        return pro.getProperty(key);
    }
    
    public static void main(String[] args) {
        String value = getValue("password");
        System.out.println(value);
    }

}

需将upload.properties放在根目录。
原文地址:https://www.cnblogs.com/xiaotang5051729/p/10154996.html