使用Properties集合存储数据,遍历取出Properties集合中的数据和Properties集合中的方法store和load
package com.yang.Test.PropertiesStudy;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* java.util.Properties集合 extends Hashtable<k,y> implements Map<k,y>
* Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。
* Properties集合是一个唯一和IO流相结合的集合
* 可以使用Properties集合中的store,把集合中的临时数据,持久化写入到硬盘中存储
* 可以使用Properties集合中的方法load,吧硬盘中保存的文件(键值对),读取到集合中使用
* 属性列表中每个键及其对应值都是一个字符串。
* Properties集合是一个双列集合,key和value默认都是字符串
*/
public class demo01 {
public static void main(String[] args) throws IOException {
show2();
}
/**
* 使用Properties集合存储数据遍历取出Properties集合中的数据
* Properties集合是一个双列集合,key和value默认都是字符串
* Properties集合有一些操作字符串的特有方法
* Object setProperty(String key, String value)调用Hashtable的方法put
* String getProperty(String key)通过key找到value值,此方法相当于Map集合中的get(key)方法
* Set<String> stringPropertyNames()返回此属性列表中的链表,其中该键及其对应值是字符串,此方法相当于Map集合中的keySet方法
*/
private static void show1() {
//创建Properties对象
Properties prop = new Properties();
//调用setProperty往集合中添加数据
prop.setProperty("赵丽颖","168");
prop.setProperty("迪丽热巴","165");
prop.setProperty("古力娜扎","160");
//使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中
Set<String> set = prop.stringPropertyNames();
//遍历Set集合,取出Properties集合的每一个键
set.forEach(key -> {
//使用getProperty方法根据key获取value
String value = prop.getProperty(key);
System.out.println(key+"="+value);
});
}
/**
* 可以使用Properties集合中的store,把集合中的临时数据,持久化写入到硬盘中存储
* void store(OutputStream out,String comments);
* void store(Writer writer,String comments);
* 参数:
* OutputStream out:字节输出流,不能写入中文
* Writer writer:字符输出流,可以写入中文
* String comments:注释,用来解释说明保存的文件是做什么用的
* 不能使用中文,会产生乱码,默认是Unicode编码
* 一般使用""空字符串
*
* 使用步骤:
* 1.创建Properties集合对象,添加数据
* 2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
* 3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
* 4.释放资源
*/
private static void show2() throws IOException {
Properties prop = new Properties();
prop.setProperty("赵丽颖","168");
prop.setProperty("迪丽热巴","165");
prop.setProperty("古力娜扎","160");
FileWriter fw = new FileWriter("Document\\1.txt");
prop.store(fw,"save data");
fw.close();
}
}
Load
```java
package com.yang.Test.PropertiesStudy;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* 可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用
* void load(InputStream inStream);
* void load(Reader reader);
* 参数:
* InputStream inStream:字节输入流,不能读取含有中文的键值对
* Reader reader;字符输入流,能读取含有中文的键值对
* 使用步骤:
* 1.创建Properties集合对象
* 2.使用Properties集合对象中的方法Load读取保存键值对的文件
* 3.遍历Properties集合
* 注意:
* 1.存储键值对的文件中,键和值默认的连接符号可以使用=,空格(其他符号)
* 2.存储键值对的文件中,键和值默认都是字符串,不用再加引号
*/
public class PropertiesStudy02 {
public static void main(String[] args) throws IOException {
//1.创建Properties集合对象
Properties prop = new Properties();
//2.使用Properties集合对象中的方法Load读取保存键值对的文件
prop.load(new FileReader("Document\\1.txt"));
//3.遍历Properties集合
Set<String> set = prop.stringPropertyNames();
set.forEach(key -> {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
});
}
}
使用Properties集合存储数据,遍历取出Properties集合中的数据和Properties集合中的方法store和load的更多相关文章
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_1_使用Properties集合存储数据,遍历取出集合中的数据
map下面的实现类叫做Hashtable Properties是唯一和IO流相结合的 讲解 代码
- java的缓冲流及使用Properties集合存取数据(遍历,store,load)
缓冲流 概述 字节缓冲流:BufferedInputStream,BufferedOutputStream 字符缓冲流:BufferedReader,BufferedWriter 缓冲流原理 缓冲区是 ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- Java集合框架(五)—— Map、HashMap、Hashtable、Properties、SortedMap、TreeMap、WeakHashMap、IdentityHashMap、EnumMap
Map Map用于保存具有映射关系的数据,因此Map集合里保存着两组值,一组值用于保存Map里的key,另一组值用于保存Map里的value,key和value都可以是任何引用类型的数据.Map的ke ...
- Properties集合中的方法store和Properties集合中的方法load
Properties集合中的方法store public class Demo01Properties { public static void main(String[] args) throws ...
- Java持久化存储对象Properties的方法list、store、load
package FileDemo; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStrea ...
- Java学习:Set接口与HashSet集合存储数据的结构(哈希表)
Set接口 java.util.Set接口 extends Collection接口 Set接口的特点: 不允许存储重复的元素 没有索引,没有带索引的方法,也不能使用普通的for循环遍历 java.u ...
- Java基础知识强化之IO流笔记45:IO流练习之 把集合中的数据存储到文本文件案例
1. 把集合中的数据存储到文本文件案例: 需求:把ArrayList集合中的字符串数据存储到文本文件 ? (1)分析:通过题目的意思我们可以知道如下的一些内容,ArrayList集合里存储的是字 ...
- 使用传统的方式遍历集合对集合中的数据进行过滤和使用Stream流的方式遍历集合对集合中的数据进行过滤
使用传统的方式,遍历集合,对集合中的数据进行过滤 class Test{ public static void main(String[] args){ ArrayList<String> ...
随机推荐
- mybatis入门,CRUD,万能Map,模糊查询
第一个Mybatis程序 核心配置文件mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?& ...
- re模块,正则表达式起别名和分组机制,collections模块,time与datetime模块,random模块
re模块和正则表达式别名和分组机制 命名分组 (1)分组--可以让我们从文本内容中提取指定模式的部分内容,用()来表示要提取的分组,需要注意的是分组 是在整个文本符合指定的正则表达式前提下进行的进一步 ...
- netty系列之:netty中常用的字符串编码解码器
目录 简介 netty中的字符串编码解码器 不同平台的换行符 字符串编码的实现 总结 简介 字符串是我们程序中最常用到的消息格式,也是最简单的消息格式,但是正因为字符串string太过简单,不能附加更 ...
- 初学者都能懂得 Git 说明
初学者都能懂得 Git 说明 本文写于 2020 年 8 月 10 日 网上有很多非常优秀的 Git 教程,但是他们都是面向有一定基础的开发者的. 可是对于没什么基础的初学者甚至是偶尔操作代码的设计师 ...
- Java 15 新特性:隐藏类
什么是隐藏类 隐藏类,是一种不能被其他类直接使用的类.引入隐藏类的主要目的是给框架来使用,使得框架可以在运行时生成类,并通过反射间接使用它们.可能有点抽象,不要紧,下面我们通过一个例子来直观的认识它! ...
- SpringCloud基础概念学习笔记(Eureka、Ribbon、Feign、Zuul)
SpringCloud基础概念学习笔记(Eureka.Ribbon.Feign.Zuul) SpringCloud入门 参考: https://springcloud.cc/spring-cloud- ...
- nacos 快速入门
每日一句 外表可是具有欺骗性的. 每日一句 No victory comes without a price. 凡是成功就要付出代价. 概述 这个快速开始手册是帮忙您快速在您的电脑上,下载.安装并使用 ...
- C++:制作火把
制作火把 时间限制 : 1.000 sec 内存限制 : 128 MB 题目描述: 小红最近在玩一个制作火把的游戏,一开始,小红手里有一根木棍,她希望能够通过这一根木棍通过交易换取制 ...
- 关于spring整合mybatis
第一步导入依赖 <dependencies> <dependency> <groupId>org.mybatis</groupId> <artif ...
- 接口开发-restful
数据库表设计 1 --员工表 2 create table Employee 3 ( 4 id NUMBER primary key, 5 employeeID NUMBER not null, 6 ...