속성을 래핑하고 파일 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() 메서드를 사용하여 속성 값을 설정하고 싶습니다. 그렇게하면 변경된 속성이 파일에 기록되지 않습니다. 하나 이상의 Property 인스턴스가 있고 다른 인스턴스는 볼 수 없기 때문에이 인스턴스를 볼 수 있습니다. 내가 원하는 것을 얻기 위해 내장 된 Properties 클래스를 확장해야한다고 생각하지만, 모든 작업을 수행하는 방법을 잘 모르겠습니다.
나는 인터넷에서 등록 정보를 사용하는 예제를 많이 보았습니다. 내가 찾지 못한 것은 클래스에서 관련된 파일 I/O를 숨기는 예제입니다. 나는 그것을 어떻게 할 것이냐 ??
1. Yoy는 p.setProperty() 대신 props.setProperty()를 호출해야합니다. 2. U는 이전 값으로 writePropertiesToFile() 메소드에서 동일한 키 "xPanelsize"를 대체하므로 반영되지 않습니다. –
'java.util.ResourceBundle'을 알고 있습니까? – EJP
모호합니다. 사용하기에 충분하지 않습니다. – dazz