B023Java学习笔记-Properties集合 - Go语言中文社区

B023Java学习笔记-Properties集合


一、Properties概述

1、API简介

①Properties 类表示了一个持久的属性集;

②Properties 可保存在流中或从流中加载;

③属性列表中每个键及其对应值都是一个字符串;

 

2、概述

①Properties集合是唯一一个跟IO结合的集合;

②可以使用Properties的store方法,把集合的临时数据,持久化地写入到硬盘中;

③可以使用Properties的load方法,把硬盘中保存的文件(键值对),读取到内存中使用;

④属性列表中每个键和对应的值都是一个字符串;

⑤Properties是双列集合,键和值默认都是字符串;

⑥Properties有一些操作字符串的特有方法;

 

3、Properties中的一些操作字符串的特有方法

①String getProperty(String key)

用指定的键在此属性列表中搜索属性;

②Object setProperty(String key, String value)

调用 Hashtable 的方法 put;

③Set<String> stringPropertyNames()

返回此属性列表中的键集;

 

4、代码演示

package study.io;

import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("大哥","185");
        properties.setProperty("二哥","182");
        properties.setProperty("三哥","179");
        properties.setProperty("小弟","168");
        Set<String> strings = properties.stringPropertyNames();
        for (String key : strings) {
            System.out.println(key+"=="+properties.getProperty(key));
            //二哥==182
            //大哥==185
            //三哥==179
            //小弟==168
        }
    }
}

 

二、Properties中的store方法

1、作用

可以将内存中的临时数据写入到硬盘中存储;

 

2、store方法

①void store(OutputStream out, String comments)

以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流;

②void store(Writer writer, String comments)

以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符;

参数说明:

OutputStream:字节输出流,不能写入中文;

Writer:字符输出流,可写入中文;

String:注释(不能使用中文,会乱码,因为默认是Unicode编码,一般使用""空字符串),用来解释说明保存的文件是做什么用的;

 

3、使用步骤

①创建Properties集合对象,添加数据;

②创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地;

③使用Properties中的store方法,可以将内存中的集合的临时数据写入到硬盘中存储;

④释放资源;

 

4、代码演示

代码:

package study.io;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //①创建Properties集合对象,添加数据;
        Properties properties = new Properties();
        properties.setProperty("大哥","185");
        properties.setProperty("二哥","182");
        properties.setProperty("三哥","179");
        properties.setProperty("小弟","168");
        //②创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地;
        FileWriter fileWriter = new FileWriter("C:\Users\Administrator\Desktop\test.txt");
        //③使用Properties中的store方法,可以将内存中的集合的临时数据写入到硬盘中存储;
        properties.store(fileWriter,"");
        //④释放资源;
        fileWriter.close();
    }
}

运行结果:

 

三、Properties中的load方法(重点)

1、作用

把硬盘中保存的文件(键值对),读取到内存中使用;

 

2、load方法

①void load(InputStream inStream)

从输入流中读取属性列表(键和元素对);

②void load(Reader reader)

按简单的面向行的格式从输入字符流中读取属性列表(键和元素对);

参数说明:

InputStream:字节输入流,不能读取带有中文的键值对集合;

Reader:字符输入流,能读取带有中文的键值对集合;

 

3、使用步骤

①创建Properties集合对象;

②使用Properties集合对象的load方法读取保存键值对的文件;

③遍历Properties集合;

 

4、注意

①存储键值对的文件中,键与值默认的连接符号可以使用=,空格(或其他符号);

②存储键值对的文件中,可以使用#进行注释,被注释的键值对不对再被读取;

③存储键值对的文件中,键与值默认都字符串,不要再加引号;

 

5、代码演示

代码:

package study.io;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //①创建Properties集合对象;
        Properties properties = new Properties();
        //②使用Properties集合对象的load方法读取保存键值对的文件;
        FileReader fileReader = new FileReader("C:\Users\Administrator\Desktop\test.txt");
        properties.load(fileReader);
        //③遍历Properties集合;
        Set<String> set = properties.stringPropertyNames();
        for (String s : set) {
            System.out.println(s+"对应的值为"+properties.getProperty(s));
        }
    }
}

运行结果:

二哥对应的值为182
大哥对应的值为185
三哥对应的值为179
小弟对应的值为168

 

 

 

 

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_29689343/article/details/96627370
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-24 22:07:50
  • 阅读 ( 1237 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢