1// 从资源文件里读取值的类,文件后缀不一定要.Properties,只要里面内容如:url=www.cnsec.net
2可通过key(url)取得值-www.cnsec.net,简单、强大
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.util.Properties;
8/** *//**
9 * ReadProperties.java
10 * Description: 读取操作属性配置文件
11 * @author li.b
12 * @version 2.0
13 * Jun 26, 2008
14 */
15public class ReadProperties {
16
17 /** *//**
18 * Description: 获取属性配置文件
19 * @param path 资源文件路径
20 * @return Properties Object
21 * @throws FileNotFoundException
22 * @throws IOException
23 */
24 public static Properties getProperties(String path) throws FileNotFoundException, IOException{
25 Properties props = null;
26 File file = new File(path);
27 if(file.exists() && file.isFile()){
28 props = new Properties();
29 props.load(new FileInputStream(file));
30 }else{
31 System.out.println(file.toString() + "不存在!");
32 }
33 return props;
34 }
35
36 /** *//**
37 * Description: 从属性文件获取值
38 * @param props Properties Object
39 * @param key
40 * @return 通过key匹配到的value
41 */
42 public static String getValue(Properties props,String key,String encod){
43 String result = "";
44 String en = "";
45 String localEN = System.getProperty("file.encoding");
46 if(encod !=null && !encod.equals("") ){
47 en = encod;
48 }else{
49 en = localEN;
50 }
51 try {
52 key = new String(key.getBytes(en),"ISO-8859-1");
53 result = props.getProperty(key);
54 if(!result.equals("")){
55 result = new String(result.getBytes("ISO-8859-1"),en);
56 }
57 } catch (Exception e) {
58 }finally{
59 if(result == null)result = "";
60 return result;
61 }
62 }
63
64 public static String getValue(Properties props,String key){
65 return getValue(props, key, "");
66 }
67
68}
posted on 2008-07-23 17:43
scea2009 阅读(410)
评论(0) 编辑 收藏