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

Properties集合中的方法store

void store(OutputStream out,String comments)
void store(Writer writer,String comments)
参数:
  OutputStream out:字节输出流,不能写入中文
  Writer writer:字符输出流,可以写入中文
  String comments:注释:用来解释说明保存的文件是做什么用的
  不能使用中文,会产生乱码,默认是Unicode编码
使用步骤:
  1.一般使用"空字符串"创建Properties集合对象,添加数据
  2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
  3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
  4.释放资源

复制代码
public static void main(String[] args) throws IOException {
        Properties a = new Properties();
        a.setProperty("是啥","111");//1.使用properties集合中的方法存储数据键与值
        a.setProperty("侵权","158");
        a.setProperty("呃呃呃","171");
        //2.创建字符输出流对象 构造方法中设置要被传入数据的文件位置
        FileWriter w  =new FileWriter("D:\\b.txt");
        //3.此方法表示将集合中的临时数据写入到w的指定文件当中
        a.store(w,"SavaMe");
 
        //4.释放资源并刷新缓冲区的字符传到文件里
        w.close();
 
        //3.1此方法表示将集合中的临时数据写入到new 出来位置的指定文件当中  区别这是字节输出流 集合里有中文会导致乱码
        a.store(new FileOutputStream(("D:\\a.txt"),true),"字节输出流");
 
 
    }
复制代码
复制代码
public class Demo01Properties {
    public static void main(String[] args) throws IOException {
        show02();
    }
private static void show02() throws IOException {
        // 1.一般使用"空字符串"创建Properties集合对象,添加数据
        Properties prop = new Properties();
        prop.setProperty("赵丽颖","167");
        prop.setProperty("迪丽热巴","169");
        prop.setProperty("古力娜扎","170");

        //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
       /* FileWriter fw = new FileWriter("day09_IOAndProperties\\prop.txt");

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

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

        //字节流,中文乱码
        prop.store(new FileOutputStream("day09_IOAndProperties\\\\prop.txt"),"");
    }
复制代码

 

Properties集合中的方法load

void load(InputStream inStream)
void load(Reader reader)
参数:
  InputStream inStream :字节输入流,不能读取含有中文的键值对
  Reader reader: 字符输入流,能读取含有中文的键值对
使用步骤:
  1.创建Properties集合对象
  2.使用Properties集合对象中的方法load读取保存键值对的文件
  3.遍历Prperties集合
注意:
  1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
  2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
  3.存储键值对的文件中,键与值默认都是字符串,不能再加引号

复制代码
public static void main(String[] args) throws IOException {
     show();
    }
    private static void show() throws IOException {
        Properties prop=new Properties();
        FileReader fr=new FileReader("D:\\b.txt");
        //使用load读取保存键值对的文件
        prop.load(fr);
        //遍历Properties集合
        Set<String> set = prop.stringPropertyNames();
        for (String key : set) {
            String value = prop.getProperty(key);
            System.out.println(key+" "+value);
        }
    }
复制代码
复制代码
public class FuXi3 {
    public static void main(String[] args) throws IOException {
        Properties a = new Properties();//a.load(new FileInputStream("C:\\Users\\shanyang\\Desktop\\22222"));12 23 ÔÚ5 此输入流会只认字节字符会乱码
        a.load(new FileReader("C:\\Users\\shanyang\\Desktop\\22222"));//此输入流会认识字符
        Set<String> strings1 = a.stringPropertyNames();
 
        for (String string : strings1) {
            String property = a.getProperty(string);
            System.out.println(string +"="+ property);
        }
 
 
    }
 
}
复制代码

 

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