您的位置:首页 > 运维架构

properties 资源文件解析工具

2013-05-01 22:53 148 查看
1
package
com.luang.util.properties;
02
import
java.io.File;
03
import
java.io.FileInputStream;
04
import
java.io.FileOutputStream;
05
import
java.io.IOException;
06
import
java.io.InputStream;
07
import
java.io.OutputStream;
08
import
java.net.URI;
09
import
java.util.Enumeration;
10
import
java.util.HashMap;
11
import
java.util.Map;
12
import
java.util.Properties;
13
import
java.util.ResourceBundle;
14
15
16
/**
17
*
18
*
PropertiesUtil.java
19
*
20
*
@descproperties资源文件解析工具
21
*
@authorGuoxp
22
*
@datatimeApr7,20133:58:45PM
23
*
24
*/
25
public
class
PropertiesUtil
{
26
27
private
Properties
props;
28
private
URI
uri;
29
30
public
PropertiesUtil(String
fileName){
31
readProperties(fileName);
32
}
33
private
void
readProperties(String
fileName){
34
try
{
35
props
=
new
Properties();
36
InputStream
fis=getClass().getResourceAsStream(fileName);
37
props.load(fis);
38
uri
=
this
.getClass().getResource(
"/dbConfig.properties"
).toURI();
39
}
catch
(Exception
e){
40
e.printStackTrace();
41
}
42
}
43
/**
44
*
获取某个属性
45
*/
46
public
String
getProperty(Stringkey){
47
return
props.getProperty(key);
48
}
49
/**
50
*
获取所有属性,返回一个map,不常用
51
*
可以试试props.putAll(t)
52
*/
53
public
Map
getAllProperty(){
54
Map
map=
new
HashMap();
55
Enumeration
enu=props.propertyNames();
56
while
(enu.hasMoreElements())
{
57
String
key=(String)enu.nextElement();
58
String
value=props.getProperty(key);
59
map.put(key,
value);
60
}
61
return
map;
62
}
63
/**
64
*
在控制台上打印出所有属性,调试时用。
65
*/
66
public
void
printProperties(){
67
props.list(System.out);
68
}
69
/**
70
*
写入properties信息
71
*/
72
public
void
writeProperties(String
key,Stringvalue){
73
try
{
74
OutputStream
fos=
new
FileOutputStream(
new
File(uri));
75
props.setProperty(key,
value);
76
//
将此Properties表中的属性列表(键和元素对)写入输出流
77
props.store(fos,
"『comments』Update
key:"
+
key);
78
}
catch
(Exception
e){
79
e.printStackTrace();
80
}
81
}
82
public
static
void
main(String[]
args){
83
PropertiesUtil
util=
new
PropertiesUtil(
"src/dbConfig.properties"
);
84
util.writeProperties(
"dbtype"
,
"MSSQL"
);
85
}
86
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: