Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
*
* @author johny
*/
public class Configuration {
public static Properties properties = new Properties();
/**
* Loading the properties
*/
public static void loadProperties(){
// Read properties file.
if(Configuration.properties.getProperty("owner") == null){
//setting of the Default properties
Configuration.properties.setProperty ( "directory","c:/temp/" ) ;
Configuration.properties.setProperty ( "owner","Johny" ) ;
}
try {
Configuration.properties.load(new FileInputStream("config.java"));
} catch (IOException e) {
}
}
/**
* Saving the Properties
*/
public static void saveProperties(){
try {
Configuration.properties.store(new FileOutputStream("config.java"), null);
} catch (IOException e) {
}
}
/**
* Setting a property
* @param strPropKey
* @param strProperty
* @return
*/
public static Object setProperty(String strPropKey, String strProperty){
return Configuration.properties.setProperty(strPropKey, strProperty);
}
/**
* Getting a property
* @param strPropKey
* @return
*/
public static String getProperty(String strPropKey){
return Configuration.properties.getProperty(strPropKey);
}
}
Ich hab mir mal das Java.util.properties angesehen und mir jetzt eine Properties klasse darum gebaut. Im Prinzip werden wenn eine Eigenschaft nicht vorhanden ist alle default Eigenschaften gesetzt.
Hat jemand evtl noch ne bessere Idee? oder Verbesserungsvorschläge?
Ziel ist die Configuration der Default Eigenschaften an einer stelle im Programmcode
ich könnte auch den default wert beim Aufruf von getProperty mit anfügen, allerdings möchte ich lieber auch die default werte ins neue config file schreiben.