Properties (Profile Class): Information that is used primarily for production configuration files and for reading configuration files.
Properties belong to the collection class and inherit from Hashtable.
Properties to note the details:
1. If the configuration file information once used in Chinese, then use the store method to generate the configuration file can only use character stream to solve, if the use of byte stream generated configuration file, the default is to use the Iso8859-1 Code table for encoding storage, this time will be garbled.
2. If the content in the properties changes, be sure to reuse the properties to generate the configuration file, or the profile information will not change.
Common methods:
1. Construction Method:
Properties()
Creates a list of empty properties with no default values.
Properties(Properties defaults)
Creates a list of empty properties with the specified default value.
2. Common methods
void load(InputStream inStream)
reads the list of attributes (key and element pairs) from the input stream.
void load(Reader reader)
reads the list of attributes (key and element pairs) from the input character stream in a simple line-oriented format.
void
store(OutputStream out, String comments)
load(InputStream)
writes the list of Properties
Properties
attributes (key and element pairs) in this table to the output stream in a format that is appropriate for the use of methods loaded into the table.
void
store(Writer writer, String comments)
load(Reader)
Properties
writes the list of attributes (key and element pairs) in this table to the output character in a format that is appropriate for using the method.
Object setProperty(String key, String value)
calls the Hashtable method put
.
String getProperty(String key)
searches for properties in this property list with the specified key.
Actual combat: The use of properties to achieve the software can only run three times, more than three times after the prompt purchase of genuine and the introduction of the JVM.
Code examples such as the following
1 ImportJava.io.File;2 ImportJava.io.FileReader;3 ImportJava.io.FileWriter;4 Importjava.io.IOException;5 Importjava.util.Properties;6 7 8 Public classdemoproperties {9 Ten Public Static voidMain (string[] args)throwsIOException { OneFile File =NewFile ("E:\\nick.property"); A if(!file.exists ()) { - file.createnewfile (); - } theProperties Properties =NewProperties (); -Properties.load (Newfilereader (file)); - intCount = 0; -String value = Properties.getproperty ("Number of logins"); + if(value!=NULL ) { -Count =Integer.parseint (value); + } Acount++; at if(count>=3) { -System.out.println ("The trial period has expired, please purchase a genuine.") "); -System.exit (0); - } - -Properties.setproperty ("Login number", count+ "" "); inProperties.store (NewFileWriter (file), "Number of logins" +count); - } to +}
View Code
Configuration file Class Properties