zoukankan      html  css  js  c++  java
  • 获取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放在根目录。
  • 相关阅读:
    做才是得到
    常用工具汇总
    迎接2017
    新年礼物
    2017
    asp.net core 日志
    板子|无向图的割点
    11/06信竞快乐模拟赛
    动态规划复习
    894D
  • 原文地址:https://www.cnblogs.com/xiaotang5051729/p/10154996.html
Copyright © 2011-2022 走看看