zoukankan      html  css  js  c++  java
  • Properties -IO相关的双列集合类

    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=u738Bu4E00u5E05
            No.1=u738Bu5C0Fu5E05
            No.3=u738Bu4E8Cu5E05
             */
    
            //关闭流
            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();
        }
    
    }
    
  • 相关阅读:
    String 尺取法
    dos 命令
    又见01背包
    01串
    Python-Matplotlib 10 面向对象 VS Matlab Style
    [ 转 ] Fiddler 教程
    Android 查看设备文件
    Python-Matplotlib 9 颜色和样式 [r]
    Python-Matplotlib 8 箱形图
    Python-Matplotlib 7 饼状图
  • 原文地址:https://www.cnblogs.com/huxiaobai/p/11593666.html
Copyright © 2011-2022 走看看