当前位置: 技术问答>java相关
关于Properties类的问题
来源: 互联网 发布时间:2015-09-01
本文导语: 代码如下: 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 prope...
代码如下:
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,程序运行后这一行就没有了。 那么属性文件里的这些注释部分该如何保留?
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,程序运行后这一行就没有了。 那么属性文件里的这些注释部分该如何保留?
|
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
句要移到saveClose() 方法里
2.property.load(inStream);
后加inStream.close();
3.property.store(outStream, null);
改为
property.store(outStream, "Real root path");
4。ok
|
String sep=System.getProperty("file.separator");
得到系统分隔符
你要把你的属性文件用load载入
得到系统分隔符
你要把你的属性文件用load载入
|
因为outStream = new FileOutputStream(S_propertyFile);这样定义的文件输出流本身就是重写原来的文件,而通过property.load(inStream)完全都忽略了注释信息,所以你原来的注释信息怎么能够保存?除非你自己另外先把它读入。实际上你的注释信息就直接写就是不要加#号嘛,这样就当作是一个键,只是值为空而以,又不会有什么影响。
你这种要保存的,最好还是用xml的好。解析虽然麻烦点,但是可以只修改部分节点并保存,而且结构更清晰,描述的内容更丰富。功能强大的多了。
你这种要保存的,最好还是用xml的好。解析虽然麻烦点,但是可以只修改部分节点并保存,而且结构更清晰,描述的内容更丰富。功能强大的多了。