IO流之Properties类
Properties类介绍
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
特点:
1、Hashtable的子类,map集合中的方法都可以用。
2、该集合没有泛型。键值都是字符串。
3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
4、有和流技术相结合的方法。

l load(InputStream) 把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
l load(Reader)
l store(OutputStream,commonts)把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
l stroe(Writer,comments);
代码演示:
/*
*
* Properties集合,它是唯一一个能与IO流交互的集合
*
* 需求:向Properties集合中添加元素,并遍历
*
* 方法:
* public Object setProperty(String key, String value)调用 Hashtable 的方法 put。
* public Set<String> stringPropertyNames()返回此属性列表中的键集,
* public String getProperty(String key)用指定的键在此属性列表中搜索属性
*/
public class PropertiesDemo01 {
public static void main(String[] args) {
//创建集合对象
Properties prop = new Properties();
//添加元素到集合
//prop.put(key, value);
prop.setProperty("周迅", "张学友");
prop.setProperty("李小璐", "贾乃亮");
prop.setProperty("杨幂", "刘恺威"); //System.out.println(prop);//测试的使用
//遍历集合
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
//通过键 找值
//prop.get(key)
String value = prop.getProperty(key);
System.out.println(key+"==" +value);
}
}
}
将集合中内容存储到文件
需求:使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作
分析:
1,创建Properties集合
2,添加元素到集合
3,创建流
4,把集合中的数据存储到流所对应的文件中
stroe(Writer,comments)
store(OutputStream,commonts)
把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
5,关闭流
代码演示:
public class PropertiesDemo02 {
public static void main(String[] args) throws IOException {
//1,创建Properties集合
Properties prop = new Properties();
//2,添加元素到集合
prop.setProperty("周迅", "张学友");
prop.setProperty("李小璐", "贾乃亮");
prop.setProperty("杨幂", "刘恺威");
//3,创建流
FileWriter out = new FileWriter("prop.properties");
//4,把集合中的数据存储到流所对应的文件中
prop.store(out, "save data");
//5,关闭流
out.close();
}
}
读取文件中的数据,并保存到集合
需求:从属性集文件prop.properties 中取出数据,保存到集合中
分析:
1,创建集合
2,创建流对象
3,把流所对应文件中的数据 读取到集合中
load(InputStream) 把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
load(Reader)
4,关闭流
5,显示集合中的数据
代码演示:
public class PropertiesDemo03 {
public static void main(String[] args) throws IOException {
//1,创建集合
Properties prop = new Properties();
//2,创建流对象
FileInputStream in = new FileInputStream("prop.properties");
//FileReader in = new FileReader("prop.properties");
//3,把流所对应文件中的数据 读取到集合中
prop.load(in);
//4,关闭流
in.close();
//5,显示集合中的数据
System.out.println(prop);
}
}
注意:使用字符流FileReader就可以完成文件中的中文读取操作了
IO流之Properties类的更多相关文章
- IO流的Properties集合,序列化流与反序列化流,打印流及commons-IO
内容介绍 Properties集合 序列化流与反序列化流 打印流 commons-IO Properties类 Properties类介绍 Properties 类表示了一个持久的属性集.Proper ...
- IO流的工具类
1.需要先导入jar包: FilenameUtils import org.apache.commons.io.FilenameUtils; public class FilenameUtilesDe ...
- IO流,File类的测试........课堂加总结
package liu0926; import java.io.File; import java.io.IOException; public class Text01 { public stati ...
- IO流--与properties集合配合使用
IO流--与properties集合配合使用: 注:生产上主要用于常量文件的配置,读取常量文件: 1:properties集合的放值与取值: /* * properties集合继承自hashTable ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_1_使用Properties集合存储数据,遍历取出集合中的数据
map下面的实现类叫做Hashtable Properties是唯一和IO流相结合的 讲解 代码
- IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)
1.File类 File类可以在程序中 操作文件和目录.File类是通过建立File类对象,在调用File类的对象来进行相关操作的. 示例: public class Demo01 { public ...
- Java IO 流--FileUtils 工具类封装
IO流的操作写多了,会发现都已一样的套路,为了使用方便我们可以模拟commosIo 封装一下自己的FileUtils 工具类: 1.封装文件拷贝: 文件拷贝需要输入输出流对接,通过输入流读取数据,然后 ...
- JAVASE(十六) IO流 :File类、节点流、缓冲流、转换流、编码集、对象流
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.File类型 1.1.File类的理解 File类是在java.io包下 File可以理解成一个文件 ...
- (22)Properties,这不会Io流中的类,但是通常和IO流中的一些流配合使用
可以和流相关联的集合对象Properties. Map |--Hashtable |--Properties Properties:该集合不需要泛型,因为该集合中的键值对都是String类型.既然是m ...
随机推荐
- 007 Android 单击事件、toast使用
第一种按钮点击事件(最常用): button=findViewById(R.id.button); button2=findViewById(R.id.button2); button.setOnCl ...
- Angular 2: 404 error occur when I refresh through the browser [duplicate]
https://stackoverflow.com/questions/35284988/angular-2-404-error-occur-when-i-refresh-through-the-br ...
- POJ_1733 Parity game 【并查集+离散化】
一.题面 POJ1733 二.分析 该题与之前做过的带权并查集的唯一区别就是数组开不下.所以需要用离散化的思想,只取那些有用的点来解决该问题. 离散化其实就是把这些所有用到的点收集后,去重,再排一下序 ...
- 113th LeetCode Weekly Contest Largest Time for Given Digits
Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour t ...
- vue实现进入页面表单聚焦
<input type="text" ref="input"/> <script> mounted(){ this.$refs['inp ...
- vue工程中使用iconfont在线CDN不生效的问题
为什么在vue工程中引入iconfont有时候不生效呢? 请依次使用以下方法 1. 在index.html中引入在线资源 <!DOCTYPE html> <html lang=&qu ...
- enum学习
https://www.cnblogs.com/hyl8218/p/5088287.html
- oracle 错误实例分析(ORA-01126)
问题描述 SQL> shutdown immediate ORA-01109: database not open Database dismounted. ORACLE instance sh ...
- python 爬虫系列04-电影天堂连接爬虫
学习的第四个爬虫 from lxml import etree import requests BASE_D = 'http://www.dytt8.net' headers = { 'User-Ag ...
- 读取日志文件,搜索关键字,打印关键字前5行。yield、deque实例
from collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen=h ...