【发布时间】:2018-03-05 07:12:11
【问题描述】:
我想创建一个包装属性并专门隐藏文件 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. U 在 writePropertiesToFile() 方法中用旧值覆盖了相同的键“xPanelsize”,所以它没有得到反映。
-
你知道
java.util.ResourceBundle吗? -
只是模糊的。不够好,不能考虑使用它。
-
@dazz 还不足以考虑使用它而不是完全重新实现它?这里有一个提示:get 意识到它。它是您问题的解决方案。它包装了
Properties,它对你隐藏了 I/O。
标签: java extend properties-file