zoukankan      html  css  js  c++  java
  • java读取properties文件

    创建PropUtil类

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropUtil {
        /**
         * 获取config文件
         * @param 
         * @return
         */
        private static Properties properties = null;
    
        public PropUtil(String path) {
            initialize(path);
        }
    
        private void initialize(String path) {
            InputStream is = getClass().getClassLoader().getResourceAsStream(path);
            if (is == null) {
    
                return;
            }
            properties = new Properties();
            try {
                properties.load(is);
            } catch (IOException e) {
    
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (Exception e) {
    
                }
            }
        }
    
        /**
         * get specified key in config files
         * 
         * @param key
         *            the key name to get value
         */
        public String get(String key) {
            String keyValue = null;
            if (properties.containsKey(key)) {
                keyValue = (String) properties.get(key);
            }
            return keyValue;
        }
    }

    创建Properties文件FrameWork.properties

    name=linchaojiang
    age=29
    code=009045

    创建测试类ReadFrameWorkProperties ,读取FrameWork.properties文件

    public class ReadFrameWorkProperties {
        
        private static PropUtil PropUtil = new PropUtil(
                "config/FrameWork.properties");
        public static String name = PropUtil.get("name");
    
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println(name);
            System.out.println(age);
            System.out.println(code);
        }
    
    }

    输出结果:

    linchaojiang

    29

    009045

  • 相关阅读:
    二、DBMS_JOB(用于安排和管理作业队列)
    Oracle 常用系统包
    DBMS_OUTPUT(用于输入和输出信息)
    C#获取当前主机硬件信息
    Centos安装mysql5.7
    Rsync安装和配置
    利用Docker编译Hadoop 3.1.0
    hadoop集群环境搭建
    axios请求、拦截器
    import时,什么时候使用花括号'{ }'
  • 原文地址:https://www.cnblogs.com/lincj/p/4770704.html
Copyright © 2011-2022 走看看