プロパティをラップし、ファイル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インスタンスがあり、1つはPropertyインスタンスには見えないためです。私は、私がやりたいことを達成するために組み込みのPropertiesクラスを拡張する必要があると思うが、それをすべて動作させる方法がわからない。
インターネットでプロパティを使用する例はたくさんあります。私が見つけていないのは、クラス内の関連するファイルI/Oを隠す例です。それはどうすればいいのですか?
1. Yoyはp.setProperty()の代わりにprops.setProperty()を呼び出す必要があります。 2. Uは、writePropertiesToFile()メソッドの同じキー "xPanelsize"を古い値でオーバーライドしているため、反映されません。 –
あなたは 'java.util.ResourceBundle'を知っていますか? – EJP
漠然としかありません。それを使用することを検討するのに十分ではありません。 – dazz