zoukankan      html  css  js  c++  java
  • JavaIO

    1. Properties

    Properties集合是双列集合

    1. 该集合中的键和值都是字符串类型
    2. 集合中的数据可以保存到流中或者从流中获取

    通常该集合操作以键值对形式存在的配置文件。

    1)获取输入流方法

    FileInputStream fis = new FileInputStream(properties文件路径);
    
    InputStream is = 类名.class.getClassLoader().getResourceAsStream(properties文件名);

    2)常用方法

    void load(InputStream in):从(文件)流中获取键值对信息,必须是字符串形式的键值对。
    
    String getProperty(String Key):用指定的键在此属性列表中搜索属性,也就是通过Key得到Key所对应的Value。
    
    void setProperty(String Key,String Value):存储元素,调用HashTable的put方法
    
    void store(OutputStream out,String comments):将集合中的键值对数据写入输出流。comments文档注释
    
    void list(PrintStream out):将集合中的键值输出到指定的输出流中
    
    void clear():清空集合中所有键值对。
    
    Set<String> StringPropertyNames():返回集合中的键集到Set集合

    2. 示例

    1)store(OutputStream out,String comments)

    将集合中的键值对数据写入输出流。

    Properties prop = new Properties();
    // 存储元素
    prop.setProperty("王五","25");
    prop.setProperty("二狗","29");
    prop.setProperty("李四","24");
    prop.setProperty("赵六","16");
    prop.setProperty("贝七","07");
    FileOutputStream fos
    =new FileOutputStream("PropertiesDemo.txt")
    prop.store(fos,
    "姓名年龄");
    // 使用集合的store方法将键值对写入输出流, fos.close();关闭流资源

    2)load(InputStream in)

    从(文件)流中获取键值对信息,必须是字符串形式的键值对。

    FileInputStream fis = new FileInputStream("PropertiesDemo.txt");
    Properties prop = new Properties();
    prop.load(fis);
    Set<String> names = prop.stringPropertyNames();
    for(String name:names){   String value = prop.getProperty(name);   System.out.println(name+":"+value); }
  • 相关阅读:
    Sectong日志分析
    风暴中心
    安全趋势:
    Mysql table ful
    linux service命令常见使用方法
    client denied by server configuration
    Ubuntu配置apache
    O2O助汪峰成功逆袭,汪峰最终上头条了
    Android切图注意事项
    2014/08/23——OJ及相关站点打开速度非常慢,训练计划login直接error!
  • 原文地址:https://www.cnblogs.com/Dm920/p/12461838.html
Copyright © 2011-2022 走看看