首页 技术 正文
技术 2022年11月11日
0 收藏 420 点赞 5,088 浏览 2920 个字

IO相关的集合类


  • java.util.Properties集合 extends hashtable(淘汰)

  • Properties类表示了一个持久的属性集。Properties可保存流中或从流中加载

  • Properties集合是一个唯一和IO流相结合的集合

    1. 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
    2. 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用
  • 属性列表中每个键及对应值都是一个字符串。

    1. Properties集合是一个双列集合,注意key和value的泛型默认是字符串
    2. Object setProperty(String key, String value) 调用 Hashtable 的方法 put。
    3. String getProperty(String key) 通过key找到value值,此方法相当于Map集合中的get(key)方法
    4. Set stringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。
    private static void show01() {
//创建Properties对象,默认字符串
Properties properties = new Properties();
//使用setProperties();添加数据
properties.setProperty("No.1","王小帅");
properties.setProperty("No.2","王一帅");
properties.setProperty("No.3","王二帅"); //使用stringPropertyNames把Properties集合中的key取出放入set集合
Set<String> set = properties.stringPropertyNames(); //遍历set集合,取出Properties集合的每一个键
for (String key:set) {
//通过getProperty方法通过key获取value
String value = properties.getProperty(key);
System.out.println(key + ":"+value);
} }

store方法


  • void store(OutputStream out,String comments)
  • void store(Writer writer,String comments)
  • 参数:
    1. OutputStream out,字节输出流,不可写中文
    2. Writer writer:字符输出流,可以写中文
    3. String comments:用来解释说明保存的文件是做什么的,不能用中文,会产生乱码,默认Unicode编码。一般comments为空字符串
  • 使用步骤
    1. 创建Properties集合对象,添加数据
    2. 创建字节/字符输出流对象,构造方法绑定要输的地
    3. 使用Properties集合中的方法store,把集合时数据,持久化写入到硬盘存储
    4. 释放资源
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;public class Propertiess {
public static void main(String[] args) {
try {
show01();
} catch (IOException e) {
e.printStackTrace();
}
} private static void show01() throws IOException {
//创建Properties对象,默认字符串
Properties properties = new Properties();
//使用setProperties();添加数据
properties.setProperty("No.1","王小帅");
properties.setProperty("No.2","王一帅");
properties.setProperty("No.3","王二帅"); //使用字符输出流,绑定输出目的地
FileWriter writer = new FileWriter("b.txt"); //使用Properties的方法store,把集合中的临时数据,持久化写入文件中
properties.store(writer,"");
/*
# -comments注释类容
#Thu Sep 26 18:21:37 CST 2019
No.2=王一帅
No.1=王小帅
No.3=王二帅
*/ //使用字节输出流
properties.store(new FileOutputStream("a.txt"),"error");
/*
#error
#Thu Sep 26 18:27:58 CST 2019
No.2=\u738B\u4E00\u5E05
No.1=\u738B\u5C0F\u5E05
No.3=\u738B\u4E8C\u5E05
*/ //关闭流
writer.close(); }}

load方法

  • 可以把Properties对象的键值对读入内存中
  • void load(InputStream inStream)
  • void load(Reader reader) ,方法使用同上
  • 区别:无comments参数
  • 注:在文件中
    1. 读取文件的键值对连接符可以是 【= , 空格】其他符号
    2. 使用#开头注释的键值对不会被读取
    3. 读取的默认是字符串,不用刻意在文字上加引号
package cn.learn.properties;import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;public class Propertiess {
public static void main(String[] args) {
try {
show01();
} catch (IOException e) {
e.printStackTrace();
}
} private static void show01() throws IOException {
//创建Properties对象,默认字符串
Properties properties = new Properties(); //使用字符输入流,绑定读取目的地
FileReader reader = new FileReader("b.txt"); //读取键值对进入集合,若有#开头的串不会被读取
properties.load(reader); //遍历查看
Set<String> strings = properties.stringPropertyNames();
for (String key:strings) {
System.out.println(key+":"+properties.getProperty(key));
/*
No.2:王一帅
No.1:王小帅
No.3:王二帅
*/
} //释放
reader.close();
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297