IO流——Properties类

package com.yy.otherio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import org.omg.Messaging.SyncScopeHelper;

public class Demo10_Properties {
/**
 * Properties是Hashtable的子类
 * Properties类表示了一个持久的属性集
 * Properties可保存在流中或从流中加载
 * 属性列表中每个键及其对应值都是一个字符串
 * Properties作为Map集合使用
 * 
 * Properties的特殊功能:
 * public Object setProperty(String key,String value)       设置键和值
 * public String getProperty(String key)                    根据键去获取值
 * public Enumeration<String> stringPropertyName()          迭代,拿到所有的键存储到枚举里去,获取到每一个键,从而获取每一个值
 * @throws IOException 
 * @throws FileNotFoundException 
 * 
 * */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        demo1();
        demo2();
        demo3();


    }

private static void demo3() throws IOException, FileNotFoundException {
    /**
     * 创建一个config.properties文件,里面写有:
     * username=wangsiqi
     * tel=15737260993
     * QQ=2312408112
     * */
    Properties wsq = new Properties();
    wsq.load(new FileInputStream("config.properties"));         //将文件上的键值对读取到集合中
//      System.out.println(wsq);
    wsq.setProperty("tel","15136981781");                       //将tel这个键,所对应的值,改为15136981781
    System.out.println(wsq);                                    //输出结果:{QQ=2312408112, tel=15136981781, username=wangsiqi} 但是源文件上的内容去没有改变
    wsq.store(new FileOutputStream("config.properties"), "xxx");//第二个参数,是对列表参数的描述,可以给值,也可以给  null,给null也就是不描述了
    /**
     * 运行结果:
     * #xxx                                 //就是上面给定的xxx
     * #Wed Jul 25 21:32:30 CST 2018        //最前面的  # 表示 :这种文件的注释
     * QQ=2312408112
     * tel=15136981781
     * username=wangsiqi

     * */
    System.out.println(wsq);                                    //输出结果:{tel=15136981781, QQ=2312408112, username=wangsiqi} 源文件的上tel会发生变化
}

private static void demo2() {
    Properties wsq = new Properties();              //创建Properties对象
    wsq.setProperty("name", "wsq");                 //设置键和值
    wsq.setProperty("QQnumber", "2312408112");      //设置键和值

//      System.out.println(wsq);                    //直接打印    输出结果:{name=wsq, QQnumber=2312408112}

    /*遍历
     * propertyNames() 返回属性列表中所有的键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键
     * */
    Enumeration<String> yy = (Enumeration<String>) wsq.propertyNames(); //获取所以的键,需要加强转,因为propertyNames()里面没有加泛型,默认都当成Object,所以要进行一次强转
    while((yy.hasMoreElements())){                  //进行遍历
      //System.out.println(yy.nextElement());       //先打印每一个键,然后再根据键去获取值
        String key = yy.nextElement();              //获取Properties中的每一个键
        String value = wsq.getProperty(key);        //根据键获取值
        System.out.println(key + "=" + value);      //运行结果:
                                                    //name=wsq
                                                    //QQnumber=2312408112
    }
}

private static void demo1() {
    Properties wsq = new Properties();              //创建Properties对象
    wsq.put("abc", 123);                            //向Properties对象wsq中存入abc和123
    System.out.println(wsq);                        //输出Properties对象wsq
                                                    //运行结果:{abc=123}
}

}

猜你喜欢

转载自blog.csdn.net/qq_41264055/article/details/81214867
今日推荐