IO-related collections
- java.util.Properties collection extends hashtable (eliminated)
- Properties class represents a persistent set of properties. Properties can be saved or loaded stream from the stream
Properties set is a unique combination of flow and a set of IO
- 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 set of read
- Each key in the list and the corresponding value of the attribute is a string.
- Properties collection is a set of double row, note that the generic default value is a string key and
- Object setProperty (String key, String value) calls the Hashtable method put.
- String getProperty (String key) to find the value value by key, this method is equivalent Map collection get (key) method
- Set
stringPropertyNames () Returns a property list a set of keys, wherein the key and its corresponding value is a string, the same name if the key is not found in the list of the main attributes, the default property list further includes a different key.
private static void show01() {
//创建Properties对象,默认字符串
Properties properties = new Properties();
//使用setProperties();添加数据
properties.setProperty("No.1","王小帅");
properties.setProperty("No.2","王一帅");
properties.setProperty("No.3","王二帅");
//使用stringPropertyNames把Properties集合中的key取出放入set集合
Set<String> set = properties.stringPropertyNames();
//遍历set集合,取出Properties集合的每一个键
for (String key:set) {
//通过getProperty方法通过key获取value
String value = properties.getProperty(key);
System.out.println(key + ":"+value);
}
}
store method
- void store(OutputStream out,String comments)
- void store(Writer writer,String comments)
- parameter:
- OutputStream out, output stream of bytes, and write Chinese
- Writer writer: character-output stream, you can write Chinese
- String comments: an explanation for the saved file is what to do, not Chinese, will be garbled, the default Unicode encoding. General comments to the empty string
- Steps for usage
- Creating Properties collection object, add data
- Create byte / character output stream object constructor bound to lose ground
- Use store Properties collection, the data collection time, written to the hard disk storage persisted
- Release resources
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Propertiess {
public static void main(String[] args) {
try {
show01();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void show01() throws IOException {
//创建Properties对象,默认字符串
Properties properties = new Properties();
//使用setProperties();添加数据
properties.setProperty("No.1","王小帅");
properties.setProperty("No.2","王一帅");
properties.setProperty("No.3","王二帅");
//使用字符输出流,绑定输出目的地
FileWriter writer = new FileWriter("b.txt");
//使用Properties的方法store,把集合中的临时数据,持久化写入文件中
properties.store(writer,"");
/*
# -comments注释类容
#Thu Sep 26 18:21:37 CST 2019
No.2=王一帅
No.1=王小帅
No.3=王二帅
*/
//使用字节输出流
properties.store(new FileOutputStream("a.txt"),"error");
/*
#error
#Thu Sep 26 18:27:58 CST 2019
No.2=\u738B\u4E00\u5E05
No.1=\u738B\u5C0F\u5E05
No.3=\u738B\u4E8C\u5E05
*/
//关闭流
writer.close();
}
}
load method
- Properties of objects can be key to read into memory
- void load(InputStream inStream)
- void load (Reader reader), using the above method
- Difference: No comments parameter
- Note: file
- Reading a file key for a connector may be a = [,] and the other symbols spaces
- # Beginning of a comment using key-value pairs can not be read
- The default read a string, do not deliberately quoted in the text
package cn.learn.properties;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Propertiess {
public static void main(String[] args) {
try {
show01();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void show01() throws IOException {
//创建Properties对象,默认字符串
Properties properties = new Properties();
//使用字符输入流,绑定读取目的地
FileReader reader = new FileReader("b.txt");
//读取键值对进入集合,若有#开头的串不会被读取
properties.load(reader);
//遍历查看
Set<String> strings = properties.stringPropertyNames();
for (String key:strings) {
System.out.println(key+":"+properties.getProperty(key));
/*
No.2:王一帅
No.1:王小帅
No.3:王二帅
*/
}
//释放
reader.close();
}
}