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

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);
        }
    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

posted @   冰灵IT  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示