我想创建一个包装属性并专门隐藏文件I/O操作的类。我在下面提供了删节的代码。这是为了从类路径之外的固定位置的文件中读取属性。它还有一个将属性写入同一个文件的方法。创建一个类来包装属性
//
/* Defines key properties of the iFlag application.
* Methods read and write properties.
*/
public class ClientProperties {
private Properties props;
private static String xPanelSizeStg = "32";
private static int xPanelSize = 32;
private static String configFilename = "/home/myname/config/client_config.properties";
public ClientProperties() {
props = new Properties();
}
/**
* Reads properties from file
* Reads the current properties object from file.
* The file is stored in /home/mimibox/config/flag_config.properties
*/
public Properties readPropertiesFromFile(){
// create and load default properties
InputStream input = null;
logger.trace("Read flag config properties.");
try {
input = new FileInputStream(configFilename);
//load a properties file from class path, inside static method
props.load(input);
//get the property values and save
xPanelSizeStg = props.getProperty("xPanelsize","32");
yPanelSizeStg = props.getProperty("yPanelsize", "32");
}
catch (IOException ex) {
logger.error("Could not open config file" + configFilename,ex);
}
finally{
if(input!=null){
try {
input.close();
}
catch (IOException e) {
logger.error("Could not close config file" + configFilename,e);
}
}
}
return props;
}
/**
* Writes properties to file
* Writes the current properties object to file.
* The file is stored in /home/mimibox/config/flag_config.properties
*/
public void writePropertiesToFile() {
//saves the current properties to file. Overwrites the existing properties.
Properties props = new Properties(); //a list of properties
OutputStream outStrm = null;
logger.info("Writing default flag config properties.");
System.out.println("Panel size x = " + xPanelSizeStg);
try {
outStrm = new FileOutputStream(configFilename);
// set the properties values
props.setProperty("xPanelsize", xPanelSizeStg);
props.setProperty("yPanelsize", yPanelSizeStg);
// save properties to file, include a header comment
props.store(outStrm, "This is the Server configuration file");
} catch (IOException io) {
logger.error("The file :{0} could not be opened", configFilename,io);
} finally {
if (outStrm!= null) {
try {
outStrm.close();
} catch (IOException e) {
logger.error("The file :{0} could not be closed", configFilename, e);
}
}
}
}
}
读取和写入方法的工作。不起作用的是试图改变一个属性的值,然后保存它。下面的演示代码成功读取属性文件并显示XPanelsize的正确值。 然后我更改该值并尝试将属性写入文件。 xPanelsize的新值64不会写入文件。
public static void main(String[] args) {
Properties props;
ClientProperties p = new ClientProperties();
props = p.readPropertiesFromFile();
String txt = props.getProperty("xPanelsize");
System.out.println("Panel size x = " + txt);
p.setProperty("xPanelsize","64"); //method not found error
p.writePropertiesToFile();
所以我想能够使用Property.setProperty()方法来设置属性的值。当我这样做时,已更改的属性不会写入文件。我可以看到这是因为我有超过1个Property实例,另一个对另一个不可见。我想我需要扩展内置的Properties类来实现我想要做的事,但我不知道如何使它全部工作。
我发现了很多在互联网上使用属性的例子。我还没有找到任何隐藏类中的相关文件I/O的例子。我会怎么做?
1. Yoy应该调用props.setProperty()而不是p.setProperty()。 2.用旧值重写方法writePropertiesToFile()中的同一个关键字“xPanelsize”,所以它没有被反映出来。 –
您是否知道'java.util.ResourceBundle'? – EJP
只有模糊。不够好考虑使用它。 – dazz