`
wisfly
  • 浏览: 63331 次
  • 性别: 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 。 用法: ...

    SpringBoot-2.7.6读取配置文件的方式

    在Spring Boot 2.7.6中,配置文件的管理是整个框架的核心功能之一,它使得开发者能够方便地...以上就是Spring Boot 2.7.6读取配置文件的基本方式及其相关知识,希望对你在理解和使用Spring Boot配置管理上有所帮助。

    保存INI配置文件和读取配置文件

    * GetPrivateProfileString:读取指定配置文件中的指定节的键名对应的字符串值。 * GetPrivateProfileInt:读取指定配置文件中的指定节的键名对应的整数值。 * WritePrivateProfileSection:写入(替换)指定配置...

    C# winform 读取修改配置文件

    2. **读取配置文件** 在C#中,可以使用`System.Configuration`命名空间的`ConfigurationManager`类来读取`appSettings`的值。以下是如何读取`Setting1`的示例代码: ```csharp using System.Configuration; // ...

    读取配置文件工具类.rar

    本压缩包包含两个工具类,分别用于读取`.yml`和`.properties`格式的配置文件,这两种格式都是Java开发中常见的配置文件类型。 首先,我们来看`.properties`配置文件。这种格式的文件历史较为悠久,它的语法简单,每...

    c++读取配置文件

    在C++编程中,读取配置文件是一项常见的任务,它允许程序在运行时根据外部文件中的设置进行调整,而不是硬编码这些参数。本教程将详细讲解如何使用C++读取配置文件,特别是针对TXT格式的文件,因为它们简单且易于...

    c语言链表实现配置信息的维护

    在读取配置文件时,逐行解析成键值对并调用`addConfigPair`;在保存配置文件时,遍历链表并以特定格式写入文件。 最后,别忘了在程序结束时释放链表中的所有内存: ```c void freeConfigList() { ConfigPair* ...

    asp.net 读取配置文件方法

    ### ASP.NET 读取配置文件方法详解 #### 一、配置文件概述 应用程序配置文件是ASP.NET项目中不可或缺的一部分,它允许开发人员在不重新编译应用的情况下更改配置设置。这些配置文件通常遵循XML格式,并且是区分大小...

    ubuntu系统读取ini配置文件

    在Ubuntu系统中,开发C++程序时经常需要处理配置文件,而INI文件因其简洁明了的格式,常被用于存储应用程序的配置参数。本模块提供了一种在Ubuntu环境下使用C++读取INI配置文件的方法,使得开发者能方便地获取和修改...

    java Properties文件key,value读取

    Java Properties文件是Java编程语言中用于存储配置信息的文本文件,通常以.key=value的形式存储键值对。这种文件格式在Java应用中广泛用于保存应用程序的配置参数、国际化字符串或者数据库连接信息等。读取...

    C++读写ini配置文件

    1. **读取配置文件**: - 类中可以定义一个成员变量来保存ini文件的内容,如`std::map, std::map, std::string&gt;&gt; data;`,用以存储节和键值对。 - 使用`std::ifstream`打开ini文件,逐行读取,根据行首的`[`判断...

    VC读取动态配置文件

    1. **读取配置文件**:`GetPrivateProfileString`用于读取配置文件中的键值。函数原型如下: ```cpp DWORD GetPrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR ...

    利用Java的Properties 类读取配置文件信息

    在Java编程中,读取配置文件是常见的任务,主要用于存储应用程序的设置或环境变量,以方便管理和维护。Java提供了一个内置的`java.util.Properties`类,用于处理`.properties`文件,这种格式通常用来存储键值对,即...

    C# .net读取ini配置文件

    通过上述方法,可以方便地使用C# .NET从INI文件中读取配置信息。这种方式不仅提高了程序的可维护性,还增强了灵活性,使得用户可以根据需要轻松调整配置。对于需要频繁更新配置的应用场景来说,使用INI文件作为配置...

    读取配置文件的简单方式

    在IT开发过程中,读取配置文件是一个常见的任务,它允许我们动态地管理应用程序的设置,如数据库连接字符串、API密钥或系统参数。本篇文章将深入探讨如何以简单的方式读取本地配置文件,并结合CListBox控件实现中...

    C++读取配置文件

    在C++编程中,读取配置文件是一项常见的任务,它允许程序员存储和加载应用程序的设置、参数或选项,而无需每次运行时重新编译代码。本篇将详细讲解如何使用C++来实现配置文件的读取功能。 首先,配置文件通常是文本...

    QT静态读取\保存配置文件类及DEMO

    在`read`方法中,你可以调用QSettings的`value`函数来读取配置项,如: ```cpp void StaticConfig::read(const QString &key, QVariant &value) { QSettings settings(QApplication::applicationDirPath() + "/...

    QSettins读取INI配置文件 - aidansen的专栏 - CSDN博客1

    在这个例子中,`settings-&gt;value("Main/SubKey1").toInt()`读取了`[main]`节(section)下的`SubKey1`键(key),并将值转换为整型。需要注意的是,路径部分("Main/SubKey1")应包含节名和键名,并且要确保与.INI...

    Qt之单例 - 读写配置文件

    为了读取和写入配置文件,我们可以添加以下方法到`ConfigInformation`类中: ```cpp // 保存设置 void ConfigInformation::setValue(const QString& key, const QVariant& value) { settings_-&gt;setValue(key, ...

    config_ini配置文件读取_

    我们可以通过`QTextStream`来读取配置文件的内容。 ```cpp QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); // 处理每一行数据,如解析键值对 } file.close(); ``` 3. **QSettings...

Global site tag (gtag.js) - Google Analytics