http://blog.9cbs.net/oxware/archive/2004/08/03/59609.aspx
Whether there is a graphical option configuration dialog, or the registry provided by the system, the local configuration file of the text is still the most secure, the most widely used configuration information saves form. The general mode of configuration information is a configuration item corresponding to a value, the former is generally a string, the latter may be a number or a string or something else. The API provided by the system in traditional Win32 programming is for us to interpret the .ini file, and then there is a packaged API in the operational registry, and there is an unprecedented method of interpretation XML in the .config file in .NET. In Java, it is also very meaningful to pack the use of the configuration file.
The encapsulation should achieve this effect: Apply only by the configuration information, set the value, set value, save and other operations, without the need to care for the specific file format, how to resolve. File format (plain text? Xml? Database?), IO method (local file? Remote file? Control 12) There will be no perception of application information information in the package class.
From the corresponding relationship between the key name and value, we are easier to think about the java.util.properties object, he is a subclass of HashTable, saving a lot of group key names - value corresponding to the two original group, and Provide fast queries and direct access to files, saved as files. Please refer to the relevant documentation for details, we look directly.
First, customize an exception:
//ConfigurationException.javapackage configuration; public class ConfigurationException extends Exception {public ConfigurationException () {} public ConfigurationException (String msg) {super (msg);}}
Then there is our package class:
//Configuration.javapackage configuration; import java.io *;. Import java.util *;. Import configuration *;. Public class Configuration {private Properties config = new Properties (); // CI private String fn = null record; // Record the configuration file name // This constructor is used to create a configuration file public configuration ()} // read the configuration information from the specified file name PUBLIC Configuration (String FileName) throws configurationException {Try {fileInputstream Fin = New FileInputStream (filename Config.Load (FIN); // Load file Fin.close ();} catch (ion ";" filename);} fn = filename; } // Specify the configuration item name, return to the configuration value public string getValue (String ItemName) {return,} // Specify the configuration item name and default value, return to the configuration value public string getValue (String ItemName, String DefaultValue ) {Return config.getProperty (itemname, defaultValue);} // set configuration item name and its value public void setValue (string itemname, string value) {config.setProperty (itemname, value); return;} // Save Profile , Specify a filename and the rise described public void saveFile (String fileName, String description) throws ConfigurationException {try {FileOutputStream fout = new FileOutputStream (fileName); config.store (fout, description); // save file fout.close ();} catch (IOException ex) {throw new ConfigurationException ( "Unable to save the specified configuration file:" fileName);}} // save the configuration file, the file name specified public void saveFile (String fileName) throws ConfigurationException {saveFile (fileName, "" );} // Save the configuration file, use the original text name public void savefile () throws configurationException {if (fn.length () == 0) Throw new configurationException ("You need to specify a saved configuration file name");
Savefile (fn);}} From this package, we can see that when instantiated objects, we can specify a file name to read configuration information, get attribute values, setValue methods to set the attribute value, savefile method Save file. Then we take a look at how to use:
//Setconfig.javaimport configuration. *; // contains this package side to use configured type IMPORT JAVA.IO. *; public class setconfig {public static void main (string [] args) {try {configure config = new configuration () ; // Set some attribute values config.setValue ("max_users_count", "50"); config.setValue ("max_openedfile_count", "20"); // Save file config.savefile ("System.conf", "Sytem Global Configuration ");} catch (configurationException ex) {// captures our custom exception EX.PrintStackTrace ();}}}
This program has created a configuration and sets two configuration items: max_users_count is 50; max_openedfile_count is 20. Finally, save this configuration as a System.conf file and add a header comment "Sytem Global Configuration". After executing, a system.conf file is generated in the directory where the program is located, we open to see what the content is in the bottom:
#Sytem Global Configuration # MON AUG 02 23:43:39 PDT 2004MAX_OPENEDFILE_COUNT = 20max_users_count = 50
It can be seen that the first line is written to our joined header comment, the second line automatically generates a time, and the following lines record configuration information in the form of
//Readconfig.javaimport configuration. *; Public class readconfig {public static void main (string [] args) {try {// Read Specify file configuration config = new configuration ("system.conf"); // Get a specific value System.out.println ("Max_USERS_COUNT") "Users Can Be Active Time Time"); // Specify the default system.out.println (config.getValue ("max_openedfile_count", "10") "Files Can Be Opened At the Same Time");} catch (configurationException ex) {ex.printstacktrace ();}}} system output
50 Users Can Be Actived At The Same Time20 Files Can Be Opened At The Same Time
Such information. It shows that we successfully read configuration information. This way our package class can be competent for ordinary configuration information operations. And for most situations, this class is also sufficient. The code here is available, you can copy directly to your own project. Last description: These attribute values can of course be changed in the file, and this is also the most common way of use of configuration files; ## indicates annotations, which can be added and deleted. The task is completed, but it is not perfect. With the biggest convenience of Properties is to have ready-made queries, settings, file access methods, but this