ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Properties集合中的方法store,Properties集合中的方法load

2022-07-13 11:03:43  阅读:139  来源: 互联网

标签:load void 键值 集合 Properties store


Properties集合中的方法store:

可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储

void store(outputStream out,string comments)

void store(lwriter writer, string comments)

参数:

OutputStream out:字节输出流,不能写入中文

writer writer:字符输出流,可以写中文

String comments:注释,用来解释说明保存的文件是做什么用的

不能使用中文,会产生乱码,认是unicode编码

一般使用“"空字符串

 

使用步骤:

1.创建Properties集合对象,添加数据

2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地

3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储

4.释放资源

public class si {
    public static void main(String[] args) throws IOException {
        show02();
    }

    public static void show02() throws IOException {
        //创建Properties集合对象,添加数据
        Properties p = new Properties();

        //使用Properties集合往集合里面添加数据
        p.setProperty("蛋挞", "egg tart");
        p.setProperty("泡芙", "puff");
        p.setProperty("tea with mike", "奶茶");

        //2.创建字节输出流/字符输出流,构造方法中绑定要输出的目的地
        FileWriter fw = new FileWriter("D://a.txt");

        //3.使用Properties集合中的方法store,把集合中的临时数据,持久写入到硬盘中
        p.store(fw, "save data");

        //4.释放资源
        fw.close();
    }
}

 

 

 

 

 

 

Properties集合中的方法load:

可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用

void load ( inputstream instream)

void Load ( Reader reader)

参数:

Inputstream instream:字节输入流,不能读取含有中文的键值对

Reader reader :字符输入流,能读取含有中文的键值对

使用步骤:

1.创建Properties集合对象

2.使用Properties集合对象中的方法Load读取保存键值对的文件

3.遍历Properties集合

public class jihe {
    public static void main(String[] args) throws IOException {
        show();
    }

    private static void show() throws IOException {
        //1.创建properties集合对象
        Properties pr = new Properties();
        //2.使用Properties集合对象中的方法load读取保存键值对的文件
        pr.load(new FileReader("nifeng.txt"));
        //prop.load(new FileInputStream("nifeng.txt"));
        //3.遍历Properties集合
        Set<String> set = pr.stringPropertyNames();
        for (String key : set){
            String prProperty = pr.getProperty(key);
            System.out.println(key+"="+prProperty);
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

搜索

复制

标签:load,void,键值,集合,Properties,store
来源: https://www.cnblogs.com/hungui/p/16473070.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有