`
wisfly
  • 浏览: 61391 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

读取配置文件及其文件中key->value值类

 
阅读更多

小伙伴们,我开了一家海淘护肤品淘宝店,搜索店铺“禾子蝶的海淘铺”,正品保证,欢迎进店选购哦。谢谢!

 

package com.cmcc.util;

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Config {
    private static Log log = LogFactory.getLog(Config.class);
    private static ResourceBundle resources=null;
    private static ResourceBundle namedResources = null;
   
    private static void getBundle(){
        try {
            resources = ResourceBundle.getBundle("system", Locale.getDefault());
        } catch (MissingResourceException mre) {
            log.error(mre);
        }
    }
   
    private static void getNamedBundle(String filename){
        try {
            namedResources = ResourceBundle.getBundle(filename, Locale.getDefault());
        } catch (MissingResourceException mre) {
            log.error(mre);
        }
    }
   
    public static String getValue(String key, String filename){
        getNamedBundle(filename);
        try{
            return namedResources.getString(key);   
        }catch(Exception e){
            return null;
        }
    }
   
    private static boolean checkResources(){
        if(resources==null)
            getBundle();
        return (resources!=null);
    }
   
   
    private static boolean changeToBoolean(String str)throws Exception{
        String tmp = str.toLowerCase();
        if(tmp.equals("true"))
            return true;
        else if(tmp.equals("false"))
            return false;
        else
            throw new Exception("不能找到资源文件");
    }
   
    public static boolean getBoolean(String key){
        String str = getString(key);
        try{
            return changeToBoolean(str);
        }catch(Exception e){
            return false;
        }
    }
   
    public static boolean getBoolean(String key,boolean defaultValue){
        String str = getString(key);
        try{
            return changeToBoolean(str);
        }catch(Exception e){
            return defaultValue;
        }
    }
   
   
    private static int changeToInt(String str)throws Exception {
        return Integer.parseInt(str);
    }
   
    public static int getInt(String key){
        String str = getString(key);       
        try{
            return changeToInt(str);
        }catch(Exception e){
            return 0;
        }          
    }
   
    public static int getInt(String key,int defaultValue){
        String str = getString(key);       
        try{
            return changeToInt(str);
        }catch(Exception e){
            return defaultValue;
        }          
    }
   
   
   
    public static String getString(String key,String defaultValue){
        String tmp = null;
        if(checkResources()){
            try{
                tmp = resources.getString(key);   
            }catch(Exception e){
                tmp = defaultValue;
            }
        }   
        return tmp;
    }
   
    public static String getString(String key){
        if(checkResources()){       
            try{
                return resources.getString(key);   
            }catch(Exception e){
                ;
            }
        }
        return null;
    }
   
    public static String[] getStringArray(String key){
        if(checkResources())
            return resources.getStringArray(key);       
        return null;
    }
   
    public static Enumeration<String> getKeys(){
        return resources.getKeys()    ;
    }
   
    public static void main(String[] args){
//        if(checkResources()){
//            Enumeration keys = Config.getKeys();
//            while(keys.hasMoreElements()){
//                String key = (String) keys.nextElement();
//                System.out.println(key + "=" + Config.getString(key));
//            }
//        }
        System.out.println(Config.getString("CHANNEL_NAME"));
    }
}

 

使用时读此类,应该可以理解,getBundle()方法中的 system 指的就是system.properties配置文件,如果你新建一个配置文件为app.properties,则读取时直接给app即可

 

小伙伴们,我开了一家海淘护肤品淘宝店,搜索店铺“禾子蝶的海淘铺”,正品保证,欢迎进店选购哦。谢谢!

分享到:
评论

相关推荐

    python-env:读取 .env 文件 (key->value) 设置值作为环境变量

    .env文件是一个文本文件,其中包含将添加到应用程序环境变量中的值。 该文件使用以下格式; 每行有一个键=值对。 安装: 通过 pip 安装: pip install python-env或通过下载包。 运行python setup.py 。 用法: ...

    java Properties文件key,value读取

    Properties文件是常用的配置文件,读取它的内容需要专门的工具类。

    SpringShiro分布式缓存版

    -- Support Shiro Annotation 必须放在springMVC配置文件中 --&gt; &lt;!-- 异常处理,权限注解会抛出异常,根据异常返回相应页面 --&gt; class="org.springframework.web.servlet.handler....

    java读取WEB-INF或src目录下的properties配置文件

    大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...

    配置文件读取

    实现可以读取如下配置文件: # Comment ; Comment [section1] key1=this is value1 key2=123 [section 2] key 1=432413

    java 读取WEB-INF下文件夹中的properties文件

    非常实用的读取配置文件的小工具,专门读取WEB-INF下文件夹中的properties文件,代码简洁、亲测没有问题,适用范围广,任何类中都可调用,传入Key及可得到Value

    ssh框架在application.xml中配置数据源所需jar

    --读取properties资源文件配置,如deploy.properties--&gt; class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:/...

    Java读写.txt文件

    用Java程序实现对.txt文件的读写。txt文件可作为配置文件,以key-Value形式出现。

    【ASP.NET编程知识】.NET Core2.1如何获取自定义配置文件信息详解.docx

    在读取配置文件信息时,我们需要使用 IConfigurationSection.GetSection 方法来获取配置节,然后使用 Value 属性来获取配置值。例如,我们可以使用以下代码来获取 API 的 Url: ```csharp public static string Get...

    纯C++封装ini配置文件的读写类(file wraper ).

    2. 支持无SECTION的 Key-value 读写. 3. 跨平台. 4. 可配置 "=" 两边需不需要空格等. 详情用法请见Test内容。 eg: CSimpleIniA ini; ini.SetUnicode(); SI_Error rc = ini.LoadFile("example.ini"); if (rc )...

    阿里巴巴nacos配置中心-C#客户端.zip

    # 阿里巴巴nacos配置中心-C#客户端 [Nacos配置中心](alibaba/nacos)的C#客户端,更多关于Nacos配置中心的介绍,可以查看[Nacos配置中心Wiki](alibaba/...nacos会在无法从配置中心查询配置文件时将读取上面的配置文件

    VC读取Ini文件例子

    VC读取Ini文件例子,建立了一个IniFile的类,可以对读取Ini文件的字符串值,整数值,建立、删除KeyValue。

    Spring MVC 入门实例

    配置 SimpleUrlHandlerMapping, 在上面的配置文件中, /hello.do 的请求将被 helloController 处理. "/hello.do"和"helloController" 是变量, 你可以更改. 但是你注意到了吗, hello.do 以 .do 作为后缀名. 如果这里...

    纯c读写ini配置文件

    纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种...

    Qt Creator 的安装和hello world 程序+其他程序的编写--不是一般的好

    if(m_ui-&gt;usrLineEdit-&gt;text()==tr("qt")&&m_ui-&gt;pwdLineEdit-&gt;text()==tr ("123456")) //判断用户名和密码是否正确 accept(); else{ QMessageBox::warning(this,tr("Warning"),tr("user name or password error!"),...

    读取ini配置文件.rar

    读取配置文件 ReadIniData(string Section, string Key, string NoText, string iniFilePath) 写入配置文件 WriteIniData(string Section, string Key, string Value, string iniFilePath)

    基于C语言的轻量级读取创建配置文件的函数库.rar

    发现读取配置文件, 还是用得比较多的. 网上搜了下, 有不少的代码范例了. 不过一般实现的函数需要传递的参数都有配置文件的路径. 个人认为在某些情况下参数传入 流 重用性更大一点. 本想基于流的参数将 读取, 添加...

    读取properties文件内容

    ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容

Global site tag (gtag.js) - Google Analytics