jam
BlogJava
首頁
新隨筆
聯系
聚合
管理
隨筆-9 評論-15 文章-1 trackbacks-0
java寫properties文件的方法
用Java寫properties文件時,如果直接用setProperties和store方法往FileOutputStream寫,寫出來的東西面目全非了。沒有正確的格式。因此我使用了這個方法來寫,一行一行的讀。一行一行地寫。
1
/**/
/*
*
2
* write property.
3
* @param title parameter defined in properties file
4
* @param key parameter defined title value
5
* @return String return value
6
*/
7
String writeProp(String filePath, String fileName, String title, String key,
8
Logger logger)
{
9
10
String strResult
=
""
;
11
String pathAddFile
=
""
;
//
write file with path and name
12
String tempFile
=
""
;
13
String strTemp
=
""
;
//
use for identify if the modify is success
14
//
filePath is null the file in the default path ,else file in the filePath+\+fileName
15
if
(filePath.equals(
""
))
{
16
pathAddFile
=
fileName;
17
tempFile
=
"
temp.properties
"
;
18
}
19
else
{
20
pathAddFile
=
filePath
+
systemSeparator
+
fileName;
21
tempFile
=
filePath
+
systemSeparator
+
"
temp.properties
"
;
22
}
23
//
properties file
24
File aFile
=
new
File(pathAddFile);
25
//
temp file
26
File tFile
=
new
File(tempFile);
27
if
(
!
aFile.exists())
{
31
strResult
=
"
error
"
;
32
return
strResult;
33
}
34
//
set property to properties
35
try
{
36
FileReader fr
=
new
FileReader(pathAddFile);
37
38
BufferedReader br
=
new
BufferedReader(fr);
39
try
{
40
FileWriter fw
=
new
FileWriter(tempFile);
41
PrintWriter
out
=
new
PrintWriter(fw);
42
43
String strLine
=
br.readLine().trim();
44
while
(strLine
!=
null
)
{
45
//
identify if strLine have title,have change key
46
if
(strLine.startsWith(title))
{
47
strLine
=
title
+
"
=
"
+
key;
48
strTemp
=
"
1
"
;
49
}
50
out
.write(strLine);
51
out
.println();
52
out
.flush();
53
//
read next line
54
strLine
=
br.readLine();
55
}
56
fw.close();
57
out
.close();
58
//
close BufferedReader object
59
br.close();
60
//
close file
61
fr.close();
62
//
delete properties file
63
if
(aFile.exists())
{
64
if
(
!
aFile.delete())
{
68
return
"
error
"
;
69
}
70
}
71
//
rename temp file to properties file
72
if
(
!
tFile.exists())
{
76
return
"
error
"
;
77
}
78
tFile.renameTo(aFile);
79
if
(
!
strTemp.equals(
"
1
"
))
{
80
//
there is no title prop exit so modify failed
85
strResult
=
"
error
"
;
86
87
}
88
return
strResult;
89
}
90
catch
(IOException ex2)
{
91
ex2.printStackTrace();
92
strResult
=
"
error
"
;
93
logger.fatal(
94
"
CmnEToyotaExtractProp ----- writeProp ----- failed !
"
);
95
return
strResult;
96
97
}
98
}
99
catch
(FileNotFoundException ex1)
{
100
ex1.printStackTrace();
103
strResult
=
"
error
"
;
104
return
strResult;
105
}
106
107
}
該方法實現了對屬性文件參數的修改。
posted on 2005-08-02 15:41
jam
閱讀(5188)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
<
2005年8月
>
日
一
二
三
四
五
六
31
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
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2005年8月 (9)
文章檔案
2005年10月 (1)
搜索
最新評論
1.?re: EJB介紹
太強大了,謝謝你。能交個朋友嗎,我的QQ是:343827585,希望能有機會跟你交流技術相關的東西。
--笨程序員
2.?re: eclipseUML 安裝
評論內容較長,點擊標題查看
--GY
3.?re: java面試題之一
評論內容較長,點擊標題查看
--jixinz
4.?re: java面試題之一
恩 5樓的很好,,,很強大```
--暗暗
5.?re: eclipseUML 安裝
被腦抽的電信封了...
--li.guofu
閱讀排行榜
1.?java面試題之一(6813)
2.?java寫properties文件的方法(5188)
3.?EJB介紹(2241)
4.?Java中實現對UTF8轉換的實現方法(1654)
5.?xml面試方面問題(1343)
評論排行榜
1.?java面試題之一(8)
2.?EJB介紹(1)
3.?隨便寫一個,java生成隨機數字(1)
4.?Java中實現對UTF8轉換的實現方法(0)
5.?java寫properties文件的方法(0)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 jam
主站蜘蛛池模板:
久久国产精品萌白酱免费
|
xvideos亚洲永久网址
|
久久久久久久免费视频
|
成人超污免费网站在线看
|
国产免费一区二区三区VR
|
国产精品亚洲αv天堂无码
|
亚洲国产精品一区第二页
|
在线观看免费污视频
|
日本不卡高清中文字幕免费
|
亚洲精品无码你懂的网站
|
亚洲国产精品无码久久久秋霞2
|
免费看内射乌克兰女
|
国产又黄又爽又大的免费视频
|
免费女人高潮流视频在线观看
|
国产一卡2卡3卡4卡2021免费观看
|
在线视频免费国产成人
|
国产日产亚洲系列
|
亚洲精品韩国美女在线
|
久久精品国产亚洲AV未满十八
|
成全视成人免费观看在线看
|
91热久久免费精品99
|
日韩a级毛片免费视频
|
亚洲精品国产品国语在线
|
亚洲一区二区三区深夜天堂
|
午夜亚洲国产精品福利
|
久久精品视频免费播放
|
成人黄18免费视频
|
亚洲妇熟XXXX妇色黄
|
亚洲日韩AV一区二区三区中文
|
久久99久久成人免费播放
|
国产免费女女脚奴视频网
|
亚洲精品国产高清嫩草影院
|
亚洲国产成人精品无码区在线秒播
|
一级**爱片免费视频
|
中文字幕免费高清视频
|
免费又黄又爽的视频
|
18gay台湾男同亚洲男同
|
亚洲中文字幕久久精品蜜桃
|
麻豆亚洲AV成人无码久久精品
|
两个人日本WWW免费版
|
无码亚洲成a人在线观看
|