zoukankan      html  css  js  c++  java
  • 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();
    

  • 相关阅读:
    理解和应用队列机制
    Visual Studio for Mac第四预
    宇宙第一开发工具
    Visual Studio 2017
    Vue开源
    Redux 和 ngrx 创建更佳的 Angular 2
    Redis缓存用起来
    C#6
    spring声明式事务 同一类内方法调用事务失效
    Spring事务管理--多个ORM框架在使用时的情况分析
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/7789068.html
Copyright © 2011-2022 走看看