`

读取修改配置文件

阅读更多
读取修改配置文件
分类: util类 2014-07-15 14:37 37人阅读 评论(0) 收藏 举报
ResourceBundle
PropertiesUtil.class.getClassLoader().getResource
PropertiesLoaderUtils.loadProperties(new ClassPathResource(propertyName));

[java] view plaincopy
package com.ffcs.aaa.utils; 
 
import java.io.BufferedWriter; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.net.URLDecoder; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 
import java.util.ResourceBundle; 
 
import org.apache.log4j.Logger; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.support.PropertiesLoaderUtils; 
 
 
public class PropertiesUtil { 
    static Logger log = Logger.getLogger("smsTaskLog"); 
    /**
     * 获取指定配置文件中所以的数据
     * 
     * @param propertyName
     *            调用方式: 1.配置文件放在resource源包下,不用加后缀
     *            PropertiesUtil.loadWarningMessage("warn_message"); 2.放在包里面的
     *            PropertiesUtil.loadWarningMessage("com.test.message");
     * @return
     */ 
    public static Map<String, String> loadProperties(String propertyName) { 
        // 获得资源包 
        ResourceBundle rb=null; 
        try {rb = ResourceBundle.getBundle(propertyName.trim()); 
        } catch (Exception e) {log.error(e.getMessage()); } 
         
        // 通过资源包拿到所有的key 
        Enumeration<String> allKey = rb.getKeys(); 
        // 遍历key 得到 value 
        Map<String, String> messageMap=new HashMap<String, String>(); 
        while (allKey.hasMoreElements()) { 
            String key = allKey.nextElement(); 
            String value = (String) rb.getString(key); 
            messageMap.put(key, value); 
        } 
        return messageMap; 
    } 
 
    /**
     * 传递键值对的Map,更新properties文件
     * 
     * @param fileName 
     *            文件名(放在resource源包目录下),需要后缀 warn_message.properties
     * @param keyValueMap
     *            键值对Map
     */ 
    public static void updateProperties(String fileName,Map<String, String> keyValueMap) { 
        //getResource方法使用了utf-8对路径信息进行了编码,当路径中存在中文和空格时,他会对这些字符进行转换,这样, 
        //得到的往往不是我们想要的真实路径,在此,调用了URLDecoder的decode方法进行解码,以便得到原始的中文及空格路径。 
        String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile(); 
        Properties props = null; 
        BufferedWriter bw = null; 
 
        try { 
            filePath = URLDecoder.decode(filePath,"utf-8");     
            log.debug("updateProperties propertiesPath:" + filePath); 
            props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName)); 
//          log.debug("updateProperties old:"+props); 
             
            // 写入属性文件 
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath))); 
             
            props.clear();// 清空旧的文件 
             
            for (String key : keyValueMap.keySet()) 
                props.setProperty(key, keyValueMap.get(key)); 
             
            log.debug("updateProperties new:"+props); 
            props.store(bw, ""); 
        } catch (IOException e) { 
            log.error(e.getMessage()); 
        } finally { 
            try { 
                bw.close(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
 
    /**
     * 判断指定配置文件中是否存在该项 Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源
     * 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启
     * 
     * @param propertyName
     *            要加上后缀:warn_message.properties
     * @param key
     * @return true:false
     */ 
    public static boolean containKey(String propertyName, String key) { 
        Properties props = null; 
        try { 
            props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(propertyName)); 
        } catch (IOException e) { 
            log.error(e.getMessage()); 
        } 
        log.debug("props content:"+props); 
        return props.containsKey(key); 
    } 
     
     
 
    private  void test() { 
        List<String> l = new ArrayList<String>(); 
        l.add("192.168.14.63");l.add("192.168.14.62");l.add("192.168.14.61"); 
 
        Map<String, String> map = new HashMap<String, String>(); 
        for (String key : l) { 
            boolean containKey = PropertiesUtil.containKey("warn_message.properties", key); 
            log.debug(key+":"+containKey); 
            map.put(key, ""); 
        } 
        PropertiesUtil.updateProperties("message.properties", map); 
        System.out.println("================================================"); 
        for (String key : l) { 
            boolean containKey = PropertiesUtil.containKey("warn_message.properties", key); 
            log.debug(key+":"+containKey); 
            map.put(key, ""); 
        } 
    } 
 
     
    public static void main(String[] args) { 
        loadProperties("a"); 
    } 
 
分享到:
评论

相关推荐

    C# winform 读取修改配置文件

    通过按钮,读取配置文件,并修改自定义配置文件appSettings中的值.这是一个很简单的dome.

    读取xml配置文件的flash导航菜单

    自做的读取xml配置文件的flash导航菜单,带fla源文件。

    unity读取excel配置文件插件

    通过此工具可以更好的在unity中读取配置文件excel格式文件信息,动态修改数据

    VC读取动态配置文件

    VC读取可以配置文件是比较简单的,但是有时候需要在配置文件增加一些项目,多数时候需要修改代码,我这个简单的例子实现了增加配置文件,但是不需要修改代码的功能,具体应用的时候可以参考思路。

    c++使用spdlog整合读取配置文件使用

    使用c++语言使用spdlog日志功能,创建新的message类进行包装,读取外部配置文件进行快速使用,好处可以不用每次都得make程序,只需要修改配置文件就可以直接运行程序

    C#读取修改Json文件

    C#读取修改Json文件

    vb读取写入修改ini配置文件示例

    用vb编写的对INI文件内容读取写入修改的例子.

    读取并正常显示配置文件,保存修改之后的信息

    读取并正常显示配置文件(.properties),保存修改之后的信息;包含源码和运行命令,

    C#读取配置文件

    .net、C#、读取指定路径的配置文件配置文件。如配置文件如下: [UserInfo] Id=1001 Name=de 可以读取其Id的值以及修改都可以的

    JAVA读取数据库的XML配置文件

    JAVA读取数据库的XML配置文件. 前言.虽然现在有HEIBERNATE开源框架做为持久层...通过读取XML配置文件来方便用户随时更改数据库配置文件... 申明:以下代码是我的研究心血,转载请保留作者名称.为了方便JSP初学者而共享...

    java类中读取配置文件内容

    在项目中我们经常要把某些常量放在配置文件中,这样修改起来会比较方便,这个工具类可以帮我们直接在java类中读取配置文件内容

    读取XML配置文件

    利用JAVA代码,读取自己添加的XML配置文件,配置文件可以随便修改,不影响代码

    调用JAR包外的配置文件

    项目中,经常把一部分功能独立出来,做一个java project,然后打成jar包供其他项目引用...这时候,如果jar包中需要读取配置文件信息,则很少把该配置打进jar包,因为它不方便修改,更多都是采用jar包读取外部配置文件。

    C++读写ini配置文件

    在原作者的基础上进行修改的,需要C请去https://download.csdn.net/download/taoyuanmin/9179573下载,或者再次修改即可

    Java 反射读取配置文件及反射类

    这个工程是使用 java 来使用反射的原理读取 *.proproies 资源文件中的一些配置信息。利用此方法可以在没有使用框架的情况下可以让你的程序更加灵活易于修改。如:可以将一些数据库配置信息,重要信息写入到资源文件...

    读取ini配置文件创建delphi界面

    通过修改或编写配置文件,来修改或创建控件属性或方法。实现界面可配置化。

    C#读取配置文件详解教程

    在C#中,可以利用配置文件来保存用户修改的信息,比如登录时保存用户的信息

    读取ini配置文件.rar

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

    C#获取和修改配置文件信息

    通过按钮,读取配置文件,并修改自定义配置文件appSettings中的值.这是一个很简单的dome.

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

    本想基于流的参数将 读取, 添加, 删除, 修改 配置文件的函数全部实现. 但发现 删除 , 修改 需要重新打开流, 单纯传入一个流参数不能方便实现. 以下是读取, 添加 配置的函数实现. "oper_config.h" #ifndef OPER_...

Global site tag (gtag.js) - Google Analytics