Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
#save data
#Wed Dec 02 20:25:04 CST 2020
菠萝吹雪=180
电灯泡=2000
上官子怡=170
我们要把这个读到硬盘中使用
| load(InputStream inStream) | 从输入字节流读取属性列表(键和元素对)。 |
---|
void | load(Reader reader) | 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。 |
---|
参数:
InputStream inStream:字节输入流,不能读取含有中文的键值对
Reader reader:字符输入流,能读取含有中文的键值对
使用步骤:
1、创建Properties集合对象,存储数据
2、使用Properties集合对象中的方法load读取保存键值对的文件
3、遍历Properties集合
注意:
1、存储键值对的文件中,键与值默认连接符可以使用=或者是空格(其他符号)
2、存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3、存储键值对的文件中,键与值默认都是字符串,不用再加引号
字符流读取
package com.baidu.属性集;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Properties;
import java.util.Set;
public class Properties集合中的方法load {
public static void main(String[] args) throws Exception {
show01();
}
private static void show01() throws Exception {
// 1、创建Properties集合对象,存储数据
Properties prop= new Properties();
// 2、使用Properties集合对象中的方法load读取保存键值对的文件
prop.load(new FileReader("F:\\develop\\prop.txt"));
// 3、存储键值对的文件中,键与值默认都是字符串,不用再加引号
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
}