TypedProperties

zhaozj2021-02-16  55

The Java.util.Properties class provides the ability to read the configuration file, but the configuration information provided by this class can only be String form, must be converted by the caller, such as transformation into an integer or Boolean. And this conversion process is also comparative, to capture exceptions. You can design a type of Properties class:

Class TypedProperties Extends Java.util.properties {

Public typedproperties ();

PUBLIC STRING GETSTRING (STRING Key) THROWS PropertyException;

Public String getString (String Key, String DefaultValue);

Public void Putstring (String Key, String Value);

Public int GetInt (String Key) THROWS NOSUCHPROPERTY

Public int GetInt (String Key, int default);

Public void Putint (String Key, Int Value);

Public long getlong (String Key) THROWS PROPERTYXCEPTION;

Public Long Getlong (String Key, long default);

Public void Putlong (String Key, long value);

Public Double GetDouble (String Key) THROWS PROPERTYEXCEPTION;

Public Double GetDouble (String Key, Double Default);

Public void Putdouble (String Key, Double Value);

Public Boolean getBoolean (String Key) Throws PropertyException;

Public Boolean getBoolean (String Key, Boolean DefaultValue);

Public void Putboolean (String Key, Boolean Value);

}

Class PropertyException Extends Exception {

}

This class and java.util.properties are a bit different, that is, it doesn't support setting the default properties, because I think that set the default attribute in a concentrated place is not good, and it is changed to pass the default properties when used. If an attribute does not have a default, it is not set in the file (or an error), then the PropertyException is thrown out.

转载请注明原文地址:https://www.9cbs.com/read-24071.html

New Post(0)