zoukankan      html  css  js  c++  java
  • 0511-Properties集合

    package A10_IOStream;
    
    import java.io.*;
    import java.util.Properties;
    import java.util.Set;
    
    /*
    java.util.Properties集合 extends Hashtable implements Map<K,V>
    Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
    Properties集合是一个唯一和流有关的集合
    store方法把集合中的临时数据,持久化写入到硬盘中存储;load方法把硬盘中保存的文件(键值对)读取到集合中使用
        void load(InputStream inStream)从输入流中读取属性列表(键和元素对)。
        void load(Reader reader)按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
        void store(OutputStream out, String comments)以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
        void store(Writer writer, String comments)以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。
    属性列表中每一个键和值都是字符串:Properties集合是一个双列集合,key和value默认都是字符串
        setProperty(String key, String value)调用 Hashtable 的方法 put。
        getProperty(String key)用指定的键在此属性列表中搜索属性。
        stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。
    */
    public class D06Properties {
        public static void main(String[] args) {
            method01();
        }
    
        //Properties集合的存取
        public static void method01() {
            Properties prop = new Properties(); //创建集合
            prop.setProperty("刘德华", "174cm");//添加元素
            prop.setProperty("周润发", "180cm");
            prop.setProperty("黎明", "178cm");
            prop.setProperty("gril苏慧", "158cm");
            Set<String> keys = prop.stringPropertyNames();//遍历读取集合
            for (String key : keys) {
                System.out.println(key + ":" + prop.getProperty(key));
            }
    /*使用store方法保存集合中的数据到硬盘文件中
            OutputStream:字节流在保存中文时因使用的是Unicode编码导致显示编码非中文
            Write:字符流可以正常处理中文
            注意:流对象使用完需释放(匿名对象自动释放)
    */
            // 字符流输出中文正常
            try (FileWriter fw = new FileWriter("properties.txt");) {  //这里使用了JDK7中try的新特性,流对象自动释放
                prop.store(fw, "properties store test");
            } catch (IOException e) {
                System.out.println(e);
            }
            //字节流输出中文:gril苏慧=158cm 会输出为——> grilu82CFu6167=158cm
            try (FileOutputStream fos = new FileOutputStream("propUnicode.txt");) {
                prop.store(fos, "properties store with unicode");
            } catch (IOException e) {
                System.out.println(e);
            }
    
    /*
        可以使用Properites集合中的load方法,把硬盘中保存的键值对文件,读取加载到集合中
        参数:
            InputStream字节输入流,不能读取含有中文的键值对
            Reader字符输入流,能读取含有中文的键值对
        使用步骤:
            1.创建Properties集合对象
            2.使用集合的load方法读取键值对文件
            3.遍历Properties集合
        注意:在存储键值对的文件中
            1.键与值的默认连接符号可以使用=,空格(其他符号)
            2.可以使用#进行注释,被注释的键值对不会再被读取
            3.键与值默认都是字符串,不用再加引号
     */
            System.out.println("----华丽的分割线----");
            Properties pps = new Properties();
            try {
                pps.load(new FileReader("properties.txt"));
                Set<String> set = pps.stringPropertyNames();
                for (String s : set) {
                    System.out.println(s + "=" + pps.getProperty(s));
                }
            } catch (IOException e) {
                System.out.println(e);
            }
            //如果使用字节流文件,经测试也是可以正常输出(老师为什么说是会乱码呢???)
            System.out.println("----华丽的分割线----");
            Properties pps1 = new Properties();
            try {
                pps1.load(new FileInputStream("propUnicode.txt"));
                Set<String> set = pps1.stringPropertyNames();
                for (String s : set) {
                    System.out.println(s + "=" + pps1.getProperty(s));
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
  • 相关阅读:
    nvm安装node的问题
    前端必读:浏览器内部工作原理
    读书笔记:《高性能网站建设指南》
    学习前端我读过的书
    Canvas绘制圆形进度条
    gitlab升级方法
    设置root远程连接ubuntu上mysql
    SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
    JSTL 格式化输出 Calendar
    在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置
  • 原文地址:https://www.cnblogs.com/sdlz/p/12869960.html
Copyright © 2011-2022 走看看