-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropsUtil.java
84 lines (73 loc) · 2.35 KB
/
PropsUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.gootrip.util;
import java.util.*;
import java.io.*;
public class PropsUtil {
//属性文件一般放到
////把db.properties放在系统属性java.home下,一般是jdk目录下的 jre目录里。
//private static final String javaHome = System.getProperty("java.home");
//private static final String FS = System.getProperty("file.separator");
//private static final String propsFileName = javaHome + FS + "destinations.properties";
/**
* 构造函数
* 找到数据源,并用这个数据源创建连接
*/
public PropsUtil() {
}
public String getPropsFilePath()
{
String filePath = this.getClass().getResource("/").getPath().toString();
filePath = filePath.substring(0, filePath.indexOf("classes")-1) + "/destinations.properties";
return filePath;
}
public InputStream getPropsIS()
{
InputStream ins = this.getClass().getResourceAsStream("/destinations.properties");
return ins;
}
/**
* 读取属性文件中的属性值
* @param attr
* @return
*/
public String readSingleProps(String attr){
String retValue = "";
Properties props = new Properties();
try {
/*if (!FileUtil.isFileExist(getPropsFilePath())) {
return "";
}
FileInputStream fi = new FileInputStream(getPropsFilePath());*/
InputStream fi = getPropsIS();
props.load(fi);
fi.close();
retValue = props.getProperty(attr);
} catch (Exception e) {
return "";
}
return retValue;
}
/**
* 读取属性文件中的属性值
* @return
*/
public HashMap readAllProps(){
HashMap h = new HashMap();
Properties props = new Properties();
try {
/*if (!FileUtil.isFileExist(getPropsFilePath()))
return new HashMap();
FileInputStream fi = new FileInputStream(getPropsFilePath());*/
InputStream fi = getPropsIS();
props.load(fi);
fi.close();
Enumeration er = props.propertyNames();
while (er.hasMoreElements()) {
String paramName = (String) er.nextElement();
h.put(paramName, props.getProperty(paramName));
}
} catch (Exception e) {
return new HashMap();
}
return h;
}
}