• 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 文件

     更新中。。。

  • 相关阅读:
    android的NDK和java进行本地socket通信
    Android 8款开源游戏引擎
    Android使用AndEngine创建第一个程序
    J2ME项目移植到Android平台六大注意事项
    面面具到!android重力传感器
    android应用程序的混淆打包
    Android 5.0最应该实现的8个期望
    android service 的各种用法(IPC、AIDL)
    Android上怎样使用《贝赛尔曲线》
    Java序列化与反序列化学习(二):序列化接口说明
  • 原文地址:https://www.cnblogs.com/Nadim/p/4684929.html
Copyright © 2020-2023  润新知