顺便今天看了一个类properties
很简单实用。是hashtable的一个子类。
Properties appProperties = new Properties();
File appConfigFile = new File();
FileInputStream appConfigStream = null;
try {
appConfigStream = new FileInputStream(appConfigFile);
appProperties.load(appConfigStream);
} catch (FileNotFoundException e) {
}
如例子所示,用这个东西来读配置文件很好用。
实用的地方是:
The load(Reader)
/ store(Writer, String)
methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream)
/ store(OutputStream, String)
methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.
The loadFromXML(InputStream)
and storeToXML(OutputStream, String, String)
methods load and store properties in a simple XML format. By default the UTF-8 character encoding is used, however a specific encoding may be specified if required. An XML properties document has the following DOCTYPE declaration:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
一些load和save时的format问题javadox有详细的描述。