Properties集合

Properties集合,它是唯一一个能与IO流交互的集合

Properties集合存值取值

 

public static void main(String[] args) throws IOException {
        //创建Properties集合
        //怎么存不一定怎么取
        Properties pro=new Properties();
        //存值
        pro.setProperty("driver", "com.mysql.jdbc.Driver");//不能存中文
        pro.setProperty("url", "jdbc.localhost");
        pro.setProperty("user", "root");
        pro.setProperty("password", "123456");
        //取值
        System.out.println(pro.getProperty("1"));
        System.out.println(pro.getProperty("2"));
        System.out.println(pro.getProperty("3"));
        //明确目的地
        FileWriter fw=new FileWriter("src/com/oracle/demo01/pro.properties");//续写加true
        pro.store(fw, "this is my properties");//不能存中文,可为空
    }

 

将集合中内容存储到文件

使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作

分析:

1,创建Properties集合

2,添加元素到集合

3,创建流

4,把集合中的数据存储到流所对应的文件中

stroe(Writer,comments)

store(OutputStream,commonts)

把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息

5,关闭流

读取文件中的数据,并保存到集合

从属性集文件prop.properties 中取出数据,保存到集合中

分析:

1,创建集合

2,创建流对象

3,把流所对应文件中的数据 读取到集合中

load(InputStream)  把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中

load(Reader)  

4,关闭流

5,显示集合中的数据

代码演示:

 

原文地址:https://www.cnblogs.com/longmingyeyu/p/12766088.html