2011年计算机二级java_2011年计算机等级考试二级Java经典算法大全(17)

从资源文件里读取值的类

从资源文件里读取值的类,文件后缀不一定要.Properties,只要里面内容如:url=www.cnsec.net

可通过key(url)取得值-www.cnsec.net,简单、强大

Java code

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

/**

* ReadProperties.java

* Description: 读取操作属性配置文件

* @author li.b

* @version 2.0

* Jun 26, 2008

*/

public class ReadProperties {

/**

* Description: 获取属性配置文件

* @param path 资源文件路径

* @return Properties Object

* @throws FileNotFoundException

* @throws IOException

*/

public static Properties getProperties(String path) throws FileNotFoundException, IOException{

Properties props = null;

File file = new File(path);

if(file.exists() && file.isFile()){

props = new Properties();

props.load(new FileInputStream(file));

}else{

System.out.println(file.toString() + "不存在!");

}

return props;

}

/**

* Description: 从属性文件获取值

* @param props Properties Object

* @param key

* @return 通过key匹配到的value

*/

public static String getValue(Properties props,String key,String encod){

String result = "";

String en = "";

String localEN = System.getProperty("file.encoding");

if(encod !=null && !encod.equals("") ){

en = encod;

}else{

en = localEN;

}

try {

key = new String(key.getBytes(en),"ISO-8859-1");

result = props.getProperty(key);

if(!result.equals("")){

result = new String(result.getBytes("ISO-8859-1"),en);

}

} catch (Exception e) {

}finally{

if(result == null)result = "";

return result;

}

}

public static String getValue(Properties props,String key){

return getValue(props, key, "");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值