1 Overview
-
Properties
Is aHashtable<k,v>
subclassHashtable<k,v>
is aMap<k,v>
class that implements the interface
Properties extends Hashtable<k.v> implements Map<k,v>
-
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 a set of IO
●store()
method, the temporary data set, writes to the hard disk persistence
●load()
method, the file stored in the hard disk (key-value pairs), using the set of read -
Properties
Collection is a double-row set, the default values and keys are strings
2. Common exclusive method
1. Add data
对象.setProperty(String key,String value)
2. Locate the key values
对象.getProperty(String key)
3. The Properties
set key out and put into a set
set of
Set<String> set = 对象.stringPropertyNames();
3. store
Methods
store(OutputStream out,String comments)
store(Writer writer,String comments)
OutputStream out output byte stream can not be written Chinese
Writer writer output character stream can be written Chinese
String comments annotated to explain the saved file is used to do not use the Chinese, generally used""
(the empty string)
- Steps for usage
- Creating
Properties
a collection of objects, add data - Create byte / character output stream object constructor output destination bound
- Using the
store()
method, the temporary data set, writes to the hard disk persistent - Release resources
package Demo01;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
public class Demo01 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileWriter writer = new FileWriter("G:\\Java\\测试文件夹\\a.txt");
properties.setProperty("1","张三");
properties.setProperty("2","李四");
properties.setProperty("3","王五");
properties.setProperty("4","赵六");
properties.store(writer,"save data");
writer.close();
}
}
4. load
Method
load(InputStream out);
load(Reader reader);
OutputStream out input stream of bytes,Can notRead containingChineseThe key to
the input character stream Reader reader,canRead containingChineseKey-value pairs
- Steps for usage
- Creating
Properties
a collection of objects - Using the
load()
method, the key to read the file - Traversal
Properties
collection
- note
- The file key, the key value for the default = connectors, spaces, or other
- Key-value pair file, you can use the # comment, the comment can not be read later
- Key-value pair file, the default key and value are strings
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Demo02 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("G:\\Java\\测试文件夹\\b.txt"));
for (String i:properties.stringPropertyNames()){
System.out.println(properties.get(i));
}
}
}