关于Properties类的问题

贝笨33 2002-09-12 02:00:11
代码如下:
package vnssys.helper;

import java.util.Properties;
import java.io.*;

public class PropertyFileHelper {

private String S_propertyFile = null;
private InputStream inStream = null;
private OutputStream outStream = null;
private Properties property = null;

public PropertyFileHelper(String S_propertyFile) throws FileNotFoundException, IOException {
try {
this.S_propertyFile = S_propertyFile;
inStream = new FileInputStream(S_propertyFile);
outStream = new FileOutputStream(S_propertyFile);
property = new Properties();
property.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("File Not Found: " + e.getMessage());
throw e;
} catch (IOException e) {
System.out.println("Error in I/O: " + e.getMessage());
throw e;
}
}

public String getProperty(String S_key, String S_defaultValue) {
return property.getProperty(S_key, S_defaultValue);

}

public void setProperty(String S_key, String S_value) {
property.setProperty(S_key, S_value);

}

public void saveClose() throws IOException {
try {
property.store(outStream, null);
} catch (IOException e) {
System.out.println("Error in I/O: " + e.getMessage());
throw e;
} finally {
try {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
}
}
}

public static void main(String args[]) {
try {
PropertyFileHelper propertyHelper = new PropertyFileHelper("D:/test.ini");
String S_path = propertyHelper.getProperty("Path","D:/");
String S_version = propertyHelper.getProperty("Version","1.0");
System.out.println(S_path);
System.out.println(S_version);
propertyHelper.setProperty("Path","E:/");
propertyHelper.setProperty("Version","2.0");
propertyHelper.saveClose();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

当程序运行一编后,test.ini文件里的内容为
#Thu Sep 12 13:36:23 CST 2002
Path=E\:/
Version=2.0

问题如下:
1.程序在运行时,为什么Path和Version输出的值仍是缺省值D:/和1.0
2.为什么保存在文件里的":"前多了一个"\"
3.我在Path=E\:/前加一行注释#Real root path,程序运行后这一行就没有了。 那么属性文件里的这些注释部分该如何保留?

...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao_yuer 2002-09-12
  • 打赏
  • 举报
回复
因为outStream = new FileOutputStream(S_propertyFile);这样定义的文件输出流本身就是重写原来的文件,而通过property.load(inStream)完全都忽略了注释信息,所以你原来的注释信息怎么能够保存?除非你自己另外先把它读入。实际上你的注释信息就直接写就是不要加#号嘛,这样就当作是一个键,只是值为空而以,又不会有什么影响。
你这种要保存的,最好还是用xml的好。解析虽然麻烦点,但是可以只修改部分节点并保存,而且结构更清晰,描述的内容更丰富。功能强大的多了。
beming 2002-09-12
  • 打赏
  • 举报
回复
store完之后outStream也应该close。
贝笨33 2002-09-12
  • 打赏
  • 举报
回复
To webwing(中国鹰派):
你的建议很好,第一个问题已经解决掉了,但注释的问题仍然未解决,因为store()方法只能在文件的头部加注释。我如果要在Version=2.0前加上一行注释#Source Version,运行了saveClose()后,这行注释就没有了,这该怎么办?
webwing 2002-09-12
  • 打赏
  • 举报
回复
1.outStream = new FileOutputStream(S_propertyFile);
句要移到saveClose() 方法里
2.property.load(inStream);
后加inStream.close();
3.property.store(outStream, null);
改为
property.store(outStream, "Real root path");
4。ok
webwing 2002-09-12
  • 打赏
  • 举报
回复
":"前多了一个"\是系统自己给":"加的转义符
wjmmml 2002-09-12
  • 打赏
  • 举报
回复
String sep=System.getProperty("file.separator");

得到系统分隔符

你要把你的属性文件用load载入

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧