ETJava Beta | Java    注册   登录
  • PropertiesUtil 配置文件读取工具类

    发表于 2024-03-28 17:24:02     阅读(191)     博客类别:J2SE

    自定义配置文件读取工具类

     

    package com.et.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    /*
    * 配置文件读取工具类
    * */
    public class PropertiesUtil {
    
        /*
         * 根据key获取指定的value
         * */
        public static String getValue(String key){
            // 1. 创建Properties对象
            Properties prop = new Properties();
            // 2. 获取文件流
            InputStream is = new PropertiesUtil().getClass().getResourceAsStream("/sys.properties");
            try {
                // 3. 加载到Properties
                prop.load(is);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return (String) prop.get(key);
        }
    
        public static void main(String[] args) {
            System.out.println(getValue("salt"));
        }
    }