zoukankan      html  css  js  c++  java
  • 属性类:Properties

    属性是程序中经常出现的形式。

    在类集中提供了一种专门的Properties类。

    public class Propertiesextends Hashtable<Object,Object>

    Properties是HashTable子类,那么肯定也是Map子类。可以使用Map的全部操作。

    但是一般情况下是单独使用的。

    设置和取得属性

    设置属性。

     Object setProperty(String key, String value)    调用 Hashtable 的方法 put。 

    得到属性:

    找到了返回值,没找到返回null。

     String getProperty(String key)    用指定的键在此属性列表中搜索属性。 

    和  找到了返回值,没找到返回defaultValue默认值。

     String getProperty(String key, String defaultValue)    用指定的键在属性列表中搜索属性。 

    验证以上操作方法:

    package 类集;
    import java.util.Properties;
    public class test1{
        public static void main(String args[]){
            Properties pro = new Properties() ;    // 创建Properties对象
            pro.setProperty("BJ","BeiJing") ;    // 设置属性
            pro.setProperty("TJ","TianJin") ;
            pro.setProperty("NJ","NanJing") ;    
            System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
            System.out.println("2、SC属性不存在:" + pro.getProperty("SC")) ;
            System.out.println("3、SC属性不存在,同时设置显示的默认值:" + pro.getProperty("SC","没有发现")) ;  //没找到返回默认值
        }
    };

    结果:

    1、BJ属性存在:BeiJing
    2、SC属性不存在:null
    3、SC属性不存在,同时设置显示的默认值:没有发现

    将以上属性写入普通文件中,字节流操作。

     void store(OutputStream out, String comments)   以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。 

    代码:

    package 类集;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    public class test1{
        public static void main(String args[]){
            Properties pro = new Properties() ;    // 创建Properties对象
            pro.setProperty("BJ","BeiJing") ;    // 设置属性
            pro.setProperty("TJ","TianJin") ;
            pro.setProperty("NJ","NanJing") ;    
            File file = new File("D:" + File.separator + "area.properteis") ;    // 指定要操作的文件
            try{
                pro.store(new FileOutputStream(file),"Area Info") ;    // 保存属性到普通文件
            }catch(FileNotFoundException e){  //异常处理。
                e.printStackTrace() ;
            }catch(IOException e){
                e.printStackTrace() ;
            }
        }
    };

    结果:

    要注意要有文件的异常处理

    既然可以保存到普通文件中,那么可以读取文件。

     void load(InputStream inStream)    从输入流中读取属性列表(键和元素对)。 

    读取文件中属性内容的代码:

    package 类集;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    public class test1{
        public static void main(String args[]){
            Properties pro = new Properties() ;    // 创建Properties对象
            File file = new File("D:" + File.separator + "area.properteis") ;    // 指定要操作的文件
            try{
                pro.load(new FileInputStream(file)) ;    // 读取属性文件
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }catch(IOException e){
                e.printStackTrace() ;
            }
            System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
            System.out.println("2、SH属性存在:" + pro.getProperty("SH")) ;
        }
    };

    输出结果:

    1、BJ属性存在:BeiJing
    2、SH属性存在:null

    保存到XML文件中。

     void storeToXML(OutputStream os, String comment)   发出一个表示此表中包含的所有属性的 XML 文档。 

    代码:

    package 类集;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    public class test1{
        public static void main(String args[]){
            Properties pro = new Properties() ;    // 创建Properties对象
            pro.setProperty("BJ","BeiJing") ;    // 设置属性
            pro.setProperty("TJ","TianJin") ;
            pro.setProperty("NJ","NanJing") ;    
            File file = new File("D:" + File.separator + "area.xml") ;    // 指定要操作的文件
            try{
                pro.storeToXML(new FileOutputStream(file),"Area Info") ;    // 保存属性到普通文件
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }catch(IOException e){
                e.printStackTrace() ;
            }
        }
    };

    结果:

    读取XML文件里的属性

     void loadFromXML(InputStream in)     将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中。 

    代码:

    package 类集;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    public class test1{
        public static void main(String args[]){
            Properties pro = new Properties() ;    // 创建Properties对象
            File file = new File("D:" + File.separator + "area.xml") ;    // 指定要操作的文件
            try{
                pro.loadFromXML(new FileInputStream(file)) ;    // 读取属性文件
            }catch(FileNotFoundException e){
                e.printStackTrace() ;
            }catch(IOException e){
                e.printStackTrace() ;
            }
            System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
        }
    };

    操作结果:

    1、BJ属性存在:BeiJing

    总结:

    1,要想进一步了解属性,可以学习后续的反射机制。了解属性应用

    2,属性里的类型肯定都是字符串。因为操作最方便。

    3,属性可以向普通文件或者XML文件中保存或者读取,按照指定格式向文件中任意扩充属性。

  • 相关阅读:
    Python 循环语句
    Python if、elif 、else语句 与 布尔运算
    Python 运算符
    Python 标识符
    Python 常用数据类型(整数,浮点数,复数,布尔型)
    Python 编辑器内容
    Python 语言介绍
    vscode 最新中文设置
    漫画数据库_基础和设计数据库
    linux配置服务器
  • 原文地址:https://www.cnblogs.com/alsf/p/6254569.html
Copyright © 2011-2022 走看看