zoukankan      html  css  js  c++  java
  • 封装的方法--读取任何路径下的properties文件中的值

       概述:我们在做项目时,经常需要从某个properties文件中读取properties文件中的值。现在我封装了一下方法,直接读取配置文件中的值。

    代码如下所示:

     1 /**
     2  * Created by qinlinsen on 2017-07-03.
     3  * 本例只要是读取任何一个.properties文件的值。如果配置文件没有的话都设有默认值,从而避免了NullPointException.
     4  */
     5 public class KSConfigurationTest {
     6     Properties properties;
     7     /**
     8      *  使用饱汉模式的双重锁模式创建一个实例
     9      */
    10     private static KSConfigurationTest ksConfigurationTest;
    11     //定义一个私有的构造方法
    12     private  KSConfigurationTest(){
    13 
    14     }
    15     public static KSConfigurationTest getKsConfigurationTest(){
    16         if(null==ksConfigurationTest){
    17             synchronized (KSConfigurationTest.class){
    18                 if(null==ksConfigurationTest){
    19                     ksConfigurationTest=new KSConfigurationTest();
    20                 }
    21             }
    22         }
    23         return  ksConfigurationTest;
    24     }
    25 
    26     /**该方法读取properties文件中的value值,在value不为空是返回value值,否则返回的是默认值。
    27      * @param key  properties文件中的key值  如文件中username=qinlinsen  其中username就是key
    28      * @param resource  properties文件所在的classpath中的路径
    29      * @param defaultValue  所设的默认值
    30      * @return 返回properties文件中的value.
    31      */
    32     public String getProperty(String key,String resource,String defaultValue){
    33         //获取ksconfig.properties文件的属性值
    34         properties=new Properties();
    35         InputStream in = KSConfigurationTest.getKsConfigurationTest().getClass().getClassLoader().getResourceAsStream(resource);
    36         try {
    37             properties.load(in);
    38         } catch (IOException e) {
    39             e.printStackTrace();
    40         }
    41         String value = properties.getProperty(key);
    42         if(value==null){
    43             return defaultValue;
    44         }
    45         return  value;
    46     }
    47     //字符串的默认值为:"";
    48     public String getProperty(String key,String resource){
    49         String value = getProperty(key, resource, "");
    50         return  value;
    51     }
    52     public int getPropertyAsInt(String key,String resource,Integer defautValue){
    53         String stringValue = defautValue.toString();
    54         String value = getProperty(key, resource, stringValue);
    55         return Integer.parseInt(value);
    56     }
    57     //设默认值为1
    58     public int getPropertyAsInt(String key,String resource){
    59         int value = getPropertyAsInt(key, resource, 1);
    60         return  value;
    61     }
    62     public static void main(String[] args) {
            //这是测试代码
    63 String key = KSConfiguration.getInstance().getProperty("app_id"); 64 System.out.println(key); 65 System.out.println("key="+key); 66 System.out.println(Boolean.valueOf("people")); 67 System.out.println(Boolean.valueOf("TRu")); 68 String app_id = KSConfigurationTest.getKsConfigurationTest().getProperty("app_id", "ksconfig.properties"); 69 System.out.println("app_id="+app_id); 70 int parsec = KSConfigurationTest.getKsConfigurationTest().getPropertyAsInt("parsec", "helloworld.properties"); 71 System.out.println("parsec="+parsec); 72 String username = KSConfigurationTest.getKsConfigurationTest().getProperty("username", "spring/test.properties"); 73 System.out.println("username="+username); 74 } 75 }
  • 相关阅读:
    NHibernate官方文档中文版--拦截器和事件(Interceptors and events)
    NHibernate官方文档中文版——批量插入(Batch inserts)
    NHibernate官方文档中文版-框架架构(Architecture)
    NHibernate官方文档中文版--ISessionFactory的配置(ISessionFactory Configuration)
    ConcurrentDictionary的key判断存在的两种方式,用错了慢100倍
    树莓派3B+,我要跑.NET Core
    C# .NET Framework4.0环境下使用async/await语法,以及其中的需要注意的地方。
    C# 利用结构体对固定格式数据进行解析
    简单工厂模式
    正则表达式替换指定格式内容,实现类似String的Format方法(用扩展方法)
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7111758.html
Copyright © 2011-2022 走看看