Java集合——Properties
Properties
属性集合,线程安全
特点
- 存储属性名和属性值
- 属性名和属性值都是字符串类型
- 没有泛型
- 和流有关
构造
方法
示例
import java.io.*;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo1 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.put("name","zhangsan");
properties.put("age","16");
properties.put("sex","男");
System.out.println(properties.toString());
Set<String> strings = properties.stringPropertyNames();
strings.forEach(x->{
System.out.println(x);
});
//流相关
PrintWriter printWriter = new PrintWriter("C:\\Users\\Syf200208161018\\Desktop\\neww.txt");
properties.list(printWriter);
printWriter.close();
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Syf200208161018\\Desktop\\neww.properties");
properties.store(fileOutputStream,null);
fileOutputStream.close();
FileReader fileReader = new FileReader("C:\\Users\\Syf200208161018\\Desktop\\neww.txt");
Properties pr = new Properties();
pr.load(fileReader);
fileReader.close();
System.out.println(pr.toString());
}
}