zoukankan      html  css  js  c++  java
  • java 配置在.properties文件中的常量

    不让用常量类,那就用.properties文件配置,放在根目录。

     1 import java.util.HashMap;
     2 import java.util.Iterator;
     3 import java.util.Map;
     4 import java.util.Properties;
     5 
     6 /**
     7  * Created by syl on 2017/12/12 0007. 读取常量properties文件
     8  */
     9 public class PropertyUtil {  
    10     @SuppressWarnings("rawtypes")
    11     private static Map map = null;  
    12   
    13     @SuppressWarnings({ "rawtypes", "unchecked" })
    14     private static void loadFile() {  
    15         map = new HashMap();  
    16         try {  
    17             Properties p = new Properties();  
    18             p.load(PropertyUtil.class.getClassLoader().getResourceAsStream("params.properties"));  
    19             Iterator it = p.keySet().iterator();  
    20             while (it.hasNext()) {  
    21                 String key = (String) it.next();  
    22                 String value = p.getProperty(key);  
    23                 map.put(key, value);  
    24             }  
    25         } catch (Exception e) {  
    26             e.printStackTrace();  
    27         }  
    28     }  
    29   
    30     public static String getValue(String str) {  
    31         if (map == null) {  
    32             loadFile();  
    33         }  
    34         return (String) map.get(str);  
    35     }  
    36     
    37     public static void main(String[] args) {
    38         String s = PropertyUtil.getValue("test");
    39         System.out.println(s);
    40     }
    41 }

     控制台输出:

  • 相关阅读:
    复制带中文的公式贴到通达信公式中显示乱码解决方案
    命令指定线程个数
    随机姓名
    将二维list某列组成新的list
    jenkins配置小结
    jmeter5.0 while controller使用总结
    centos7 桌面化配置
    django study1 数据库
    centos7 安装firefox
    python之self
  • 原文地址:https://www.cnblogs.com/arrrrrya/p/8029453.html
Copyright © 2011-2022 走看看