• Java日志第39天 2020.8.13


    Properties

    java.util.Properties extends Hashtable<k, v> implements Map<k, v>

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

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

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

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

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

      Properties集合是一个双列集合,key和value默认都是字符串

    import java.util.Properties;
    import java.util.Set;
    
    public class Demo01Properties {
    
        public static void main(String[] args) {
            //创建Properties集合对象
            Properties prop = new Properties();
            //使用setProperty往集合中添加数据
            prop.setProperty("迪丽热巴","165");
            prop.setProperty("古力娜扎","164");
            prop.setProperty("马尔扎哈","163");
            prop.setProperty("撒扬娜拉","162");
            //使用stringPropertyName把集合中能够的键取出,存储至一个Set集合中
            Set<String> set = prop.stringPropertyNames();
            //遍历set集合,取出Properties集合的每一个键
            for (String key : set) {
                String value = prop.getProperty(key);
                System.out.println(key + "=" + value);
            }
        }
    }

    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.释放资源

    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Demo02Properties {
        public static void main(String[] args) throws IOException {
            //创建Properties集合对象,添加数据
            Properties prop = new Properties();
            prop.setProperty("迪丽热巴","165");
            prop.setProperty("古力娜扎","164");
            prop.setProperty("马尔扎哈","163");
            prop.setProperty("撒扬娜拉","162");
            //创建字节输出流/字符输出流对象,构造方法中要绑定输出的路径
            FileWriter fw = new FileWriter("E:\Java\Practice\src\Practice\demo15\d.txt");
    
            //使用Properties集合中的方法store,把集合中的临时数据持久化写入到硬盘中存储
            prop.store(fw,"save data");
            
            //释放资源
            fw.close();
        }
    }

    结果如下:

    load方法

    - void load(InputStream inStream)

    - void load(Reader reader)

    参数:

    InputStream inStream:字节输入流,不能读取含有中文的键值对

    Reader reader:字符输入流,能读取含有中文的键值对

    使用步骤:

    1.创建Properties集合对象

    2.使用Properties集合对象中的方法load读取保存键值对的文件

    3.遍历Properties集合

    注意:

    1.存储键值对的文件,键与值默认的连接符号可以使用=,空格

    2.存储键值对的文件,可以使用#进行注释,被注释的键值对不会再被读取

    3.存储键值对的文件,键与值默认都是字符串,不用再加引号

    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.Set;
    
    public class Demo03Properties {
        public static void main(String[] args) throws IOException {
            //创建Properties集合对象
            Properties prop = new Properties();
    
            //使用Properties集合对象中的方法load读取保存键值对的文件
            prop.load(new FileReader("E:\Java\Practice\src\Practice\demo15\d.txt"));
    
            //遍历Properties集合
            Set<String> set = prop.stringPropertyNames();
    
            for (String key : set) {
                String value = prop.getProperty(key);
                System.out.println(key + "=" + value);
            }
        }

    结果如下:

  • 相关阅读:
    py-day1-2 python的循环语句
    B/S和C/S结构的区别
    php get_magic_quotes_gpc() addslashes()
    SqlHelper数据库访问类
    随滚动条滚动的居中div
    有关Repeater的事件
    Repeater的ItemCommand事件和ItemCreated事件,高手请跳过~
    温故而知新之数据库的分离和附加…高手请跳过….
    自己做的一个小功能~
    php什么是变量的数据类型
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13498951.html
Copyright © 2020-2023  润新知