关于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,程序运行后这一行就没有了。 那么属性文件里的这些注释部分该如何保留?