zoukankan      html  css  js  c++  java
  • JAVA访问配置文件总结

    一、全局配置的简单 propertie 文件实现

     1 package com.testgs.utils;
     2 
     3 import java.util.*;
     4 import java.io.*;
     5 
     6 
     7 public final class ARConfig {
     8 
     9     private Properties conf = new Properties();
    10     private String prefix = "";
    11     /**
    12      * 全局配置文件名
    13      */
    14     public static final String GLOBAL_CONF_FILE = "/analysisReportConfig.properties";
    15 
    16     public ARConfig(String prefix) {
    17         this.prefix = prefix;
    18         loadConf();
    19     }
    20 
    21     /**
    22      * 取得属性文件实例
    23      * @param prefix 各数据库连接前缀
    24      * @return
    25      */
    26     public synchronized static ARConfig getInstance(String prefix) {
    27         return new ARConfig(prefix);
    28     }
    29     
    30     public String getConfString(String name, String defaultValue) {
    31         String result = getConfString(name);
    32         result = (result == null) ? defaultValue : result;
    33         return result;
    34     }
    35 
    36     /**读取配置信息的 boolean 值
    37      * @param name
    38      * @param defaultValue
    39      * @return
    40      */
    41     public boolean getConfBoolean(String name, boolean defaultValue) {
    42         boolean result = defaultValue;
    43         String value = getConfString(name);
    44         if (value != null) {
    45             value = value.toLowerCase();
    46             result = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
    47         }
    48         return result;
    49     }
    50 
    51     /**读取配置信息的 boolean 值,如果没有,默认为 false
    52      * @param name
    53      * @return
    54      */
    55     public boolean getConfBoolean(String name) {
    56         return getConfBoolean(name, false);
    57     }
    58 
    59     /**
    60      * 读取配置信息的 int 值
    61      * @param name
    62      * @param defaultValue
    63      * @return
    64      */
    65     public int getConfigInt(String name, int defaultValue) {
    66         String intV = getConfString(name);
    67         int result = defaultValue;
    68         if (intV != null) {
    69             try {
    70                 result = Integer.parseInt(intV.trim());
    71             } catch (Exception e) {
    72                 e.printStackTrace();
    73             }
    74         }
    75         return result;
    76     }
    77 
    78     public String getConfString(String name) {
    79         name = this.prefix + name;
    80         return conf.getProperty(name);
    81     }
    82 
    83     protected synchronized void loadConf() {
    84         conf.clear();
    85         InputStream input = null;
    86         try {
    87             input = this.getClass().getResourceAsStream(GLOBAL_CONF_FILE);
    88             conf.load(input);
    89         } catch (IOException e) {
    90             throw new RuntimeException("找不到配置文件: " + GLOBAL_CONF_FILE);
    91         } finally {
    92             if (input != null)
    93                 try {
    94                     input.close();
    95                 } catch (Exception closeE) {
    96                 }
    97         }
    98     }
    99 }
    访问 properties 文件

     更新中。。。

  • 相关阅读:
    Angular总结一:环境搭建
    适应移动端的地址四级(省市区街道)联动选择
    插入换行符
    自定义input[type="checkbox"]的样式
    使用zepto实现QQ消息左滑删除效果
    windows 下更新 npm 和 node
    [attribute |= value] 与 [attribute ^= value],[attribute ~= value] 与 [attribute *= value] 的联系与区别
    小程序父子组件onLoad和Created之间的问题
    小程序行内元素且有border的情况下,根据文字宽度自动调节元素宽度
    块级元素水平居中
  • 原文地址:https://www.cnblogs.com/Nadim/p/4684929.html
Copyright © 2011-2022 走看看