1、集合存储数据
java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。
Properties集合是一个唯一和IO流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
public class Demo01Properties {
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("赵丽颖", "168");
prop.setProperty("地理热巴", "165");
// stringPropertyNames返回属性列中的键集,其中该键及其对应值是字符串
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
System.out.println(key);
}
}
}
2、Properties-store
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
public class Demo02Properties {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.setProperty("赵丽颖", "168");
prop.setProperty("地理热巴", "165");
prop.setProperty("鼓励娜扎", "160");
FileWriter fw = new FileWriter("b.txt");
prop.store(fw, "savedata");
fw.close();
}
}
3、Properties-load
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
void load(InputStream inStream)
void load(Reader reader)
1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3.存储键值对的文件中,键与值默认都是字符串,不用再加引号
public class Demo03Properties {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.load(new FileReader("b.txt"));
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
System.out.println(key);
}
}
}






网友评论