• Java IO流-Properties


    2017-11-05 21:37:50

    • Properties

    Properties:Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

            Properties是一个属性集合类,是一个可以和IO流相结合的使用的集合类。

            Properties类 可以保存在流中或者从流中加载,是Hashtable的子类,也就是Map的子类。

    *构造方法

    *常用方法

    ~ Properties作为Map集合的使用

    public class Main {
        public static void main(String[] args) {
            //没有泛型,不是泛型类
            Properties prop = new Properties();
    
            //添加元素
            prop.put("1","hello");
            prop.put("2","world");
            prop.put("3","!");
    
            //遍历集合
            Set<Object> set = prop.keySet();
            for(Object k:set){
                Object val = prop.get(k);
                System.out.println(k+"---"+val);
            }
        }
    }
    

     ~ Properties的特殊功能

    1. public Object setProperty(String key,String value)
    2. public String getProperty(String key)
    3. public Set<String> stringPropertyNames()
    public class Main {
        public static void main(String[] args) {
            //没有泛型,不是泛型类
            Properties prop = new Properties();
    
            //添加元素
            prop.setProperty("1","hello");
            prop.setProperty("2","world");
            prop.setProperty("3","!");
    
            //遍历集合
            Set<String> set = prop.stringPropertyNames();
            for(String k:set){
                String val = prop.getProperty(k);
                System.out.println(k+"---"+val);
            }
        }
    }
    

    ~ Properties与IO流的结合

    1. public void load(Reader reader):把文件中的数据读取到Properties集合中,文件中的数据必须是键值对形式的。
    2. public void store(Writer writer,String comments):把集合中的数据存储到文件中。
            //没有泛型,不是泛型类
            Properties prop = new Properties();
    
            //添加元素
            prop.setProperty("1","hello");
            prop.setProperty("2","world");
            prop.setProperty("3","!");
    
            Writer w = new FileWriter("E:/test.txt");
            prop.store(w,"helloworld");
    
    #helloworld
    #Sun Nov 05 22:06:16 CST 2017
    1=hello
    2=world
    3=!
    
            Properties prop = new Properties();
            Reader r = new FileReader("E:/test.txt");
            prop.load(r);
            r.close();
    

  • 相关阅读:
    Qt计算器开发(三):执行效果及项目总结
    [HNOI2019]校园旅行
    How to fix nuget Unrecognized license type MIT when pack
    How to fix nuget Unrecognized license type MIT when pack
    git 通过 SublimeMerge 处理冲突
    git 通过 SublimeMerge 处理冲突
    git 上传当前分支
    git 上传当前分支
    gif 格式
    gif 格式
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/7789068.html
Copyright © 2020-2023  润新知