读取修改配置文件

读取修改配置文件
分类: 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");
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WinForm中读取Oracle配置文件,可以按照以下步骤进行: 1. 首先,确保已经正确安装了Oracle客户端,并配置好了环境变量。这样才能在WinForm中正确访问Oracle数据库。 2. 在WinForm的项目中添加一个配置文件,可以命名为“App.config”,这个文件将用于存储Oracle数据库的连接信息。 3. 在配置文件中添加以下代码,用于配置Oracle数据库的连接信息: ```xml <configuration> <appSettings> <add key="OracleConnectionString" value="Data Source=数据库地址;User ID=用户名;Password=密码;"/> </appSettings> </configuration> ``` 其中,将“数据库地址”替换为实际的Oracle数据库地址、“用户名”替换为要连接的数据库的用户名、“密码”替换为对应的密码。 4. 在WinForm中的代码中,可以通过以下方法读取配置文件中的连接信息: ```csharp string connectionString = ConfigurationManager.AppSettings["OracleConnectionString"]; ``` 这样就可以获取到配置文件中存储的Oracle数据库的连接字符串。 5. 使用获取到的连接字符串进行数据库操作,例如连接数据库、执行SQL语句等。 需要注意的是,配置文件中的连接信息可以根据实际情况进行修改,以适应不同的Oracle数据库连接需求。另外,在代码中访问配置文件需要引用`System.Configuration`命名空间。 以上就是在WinForm中读取Oracle配置文件的基本步骤。通过配置文件,在不同环境下可以方便地修改连接信息,使得程序更加灵活和易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值