An XML or *.properties file in Java to read the user options (in NetBeans 7.0)
For a tool development, I wish to have basic information such as InputPath, OutputPath, DB server name (username, pwd and DB开发者_Python百科name) etc configured before the user uses the tool.
Can it be easier to have this information in XML file and read this file from java code? or create an *.properties file and use it.
I am using netbeans 7.0 version.
The idea is to have default information will be used from the file, if the user didnt modify the default info.
Also, the xml which used to store the default info shall be available to the user for updating it.
Thank you, Ramm
Either approach is quite acceptable.
The Properties class in the standard libraries will allow you to use the properties format or xml format with very few code changes.
The .properties approach
Properties props = new Properties();
props.load(new FileInputStream("user.properties"));
//get a value with a default of NOTDEFINED if there is no value found.
String dbPath = props.getProperty("databasePath","NOTDEFINED");
The .xml approach
Properties props = new Properties();
props.loadFromXML(new FileInputStream("user.properties"));
String dbPath = props.getProperty("databasePath","NOTDEFINED");
精彩评论