javaSE.Properties

Properties

属性集合
特点:

  • 存储属性名和属性值;
  • 属性名和属性值都是字符串类型;
  • 没有泛型;
  • 和流有关;
/**
 * 演示Properties
 */
public class PropertiesDemo {
    public static <FileIutputStream> void main(String[] args) throws Exception{
        //1.创建集合
        Properties properties = new Properties();
        //2.添加属性
        properties.setProperty("name", "zs");
        properties.setProperty("age", "20");
        //3.1 keySet遍历
        Set<Object> objects = properties.keySet();
        //3.2 entrySet遍历
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object,Object> entry:entries){
            System.out.println("entrySetb遍历:"+entry.getKey()+"="+entry.getValue());
        }
        //3.3 stringPropertyNames()遍历
        Set<String> propnames = properties.stringPropertyNames();
        for (String propname:propnames){
            System.out.println("stringPropertyNames()遍历:"+propname+"="+properties.getProperty(propname));
        }
        //4.和流有关的方法
        System.out.println("---4.1 list()------");
        PrintWriter pw = new PrintWriter("d:\\print.txt");
        properties.list(pw);
        pw.close();
        System.out.println("---4.2 store()------");
        FileOutputStream fos = new FileOutputStream("d:\\store.properties");
        properties.store(fos,"comments");
        fos.close();
        System.out.println("---4.3 load()--------");
        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("d:\\store.properties");
        properties1.load(fis);
        System.out.println("load():"+properties1.toString());
        fis.close();
    }
}
posted @ 2022-03-17 12:08  老李学Java  阅读(24)  评论(0)    收藏  举报