Properties Overview
java.util.Properties class inherits from Hashtable, to represent a persistent set of properties. It uses key-value data structure is stored, each key and its corresponding value is a string. This class is also used by many Java classes, when such acquisition system attributes, System.getProperties method is to return a Properties object Properties class represents a persistent set of properties. Properties can be saved or loaded from the stream in the stream. Properties set is a unique combination of flow and IO set. The method may be used to store Properties set, the temporary data set, is written into persistent storage drives. The method may be used in load Properties set to save the file in a hard disk (key-value pairs), using the read set. Each key and its corresponding value of the property list is a string. Properties set is a dual column set, key, and the default value is a string.
Construction method
- public Properties (): Creates an empty list of attributes.
The basic method of storage
- public Object setProperty (String key, String value): save one pair of attributes. Hashtable method is to call the bottom of the put.
- public String getProperty (String key): search using the key property value of this property specified in the list. This method is equivalent Map collection get (key) method
- The name of the collection of all keys: public Set stringPropertyNames (). This method is equivalent Map collection method keySet
Methods with respect to flow
Properties collection method Store , the temporary data set, persisted written into the hard disk storage
- void store(OutputStream out, String comments)
- void store(Writer writer, String comments)
Detailed method parameters:
- OutputStream out: output stream of bytes can not be written Chinese, distortion.
- Writer writer: character-output stream, you can write Chinese. A Chinese is a byte
- String comments: annotation to help illustrate the saved file is what to do with. You can not use Chinese, will be garbled, the default is Unicode encoding. General use "" empty string
Steps for usage:
- Creating Properties collection object, add data
- Create an output stream of bytes / character output stream objects, the method is configured to output destination of bound
- Use store Properties set, the temporary data set, is written into persistent storage drives
- Release resources
Code Example
Package demo05; Import java.io.FileWriter; Import java.io.IOException; Import the java.util.Properties; public class Demo01Properties { public static void main (String [] args) throws IOException { // 1. Create Properties collection object, adding data the Properties prop = new new the Properties (); prop.setProperty ( "Zhao Liying", "168" ); prop.setProperty ( "Dilly Reba", "165" ); prop.setProperty ( "Gülnezer Bextiyar" "160" ); // 2. Create an output stream of bytes / character output stream object,Destination constructor bound to be output FW = FileWriter new new FileWriter ( "\\ day20 prop.txt" ); // 3. Use store Properties set, the temporary data set, writes to the hard disk persistent storage prop.store (fw, " Data Save " ); // 4. release resources fw.close (); } }
Properties collection method Load , save hard disk file (key-value pairs), the read set using
- void load(InputStream inStream)
- void load(Reader reader)
Detailed method parameters:
- InputStream inStream: input stream of bytes, the key can not be read to contain Chinese
- Reader reader: character-input stream that can be read on the key containing Chinese
Steps for usage:
- Creating Properties collection object
- File Usage Properties collection object load read the saved key-value pairs
- Traversing the Properties collection
note:
- Key-value pairs stored in the file, the default value of the key symbols can be used = is connected, the space (other symbols)
- File storage key-value pairs, you can use # annotated, annotated key-value pairs will not be read
- File storage key-value pairs, the default key and value are strings, do not add quotes
Code Example
Package demo05; Import java.io.FileReader; Import java.io.IOException; Import the java.util.Properties; Import java.util.Set; public class Demo02Properties { public static void main (String [] args) throws IOException { // 1. Create a set of objects Properties Properties prop; prop = new new Properties (); // 2. use Properties collection object to read the file stored load value pairs prop.load ( new new the FileReader ( "\\ prop.txt day20 " )); //prop.load(new FileInputStream("09_IOAndProperties\\prop.txt")); //3.遍历Properties集合 Set<String> set = prop.stringPropertyNames(); for (String key : set) { String value = prop.getProperty(key); System.out.println(key + "=" + value); } } }