zoukankan      html  css  js  c++  java
  • java 读取Properities 文件

    今天看了下java的Properties 的这个东东,平时都在上班,忙捏。 

    基本属性

    1)load(InputStream inStream)

       这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象如下面的代码:

    Properties pro = new Properties();
    FileInputStream in = new FileInputStream("a.properties");
    pro.load(in);
    in.close();

    getProperty(String key)

    setProperty(String key,String value)

    store(OutputStream out, String comments)

       这个方法将Properties类对象的属性列表保存到输出流中如下面的代码:

    FileOutputStream oFile = new FileOutputStream(file, true);
    pro.store(oFile, "Comment"); Comment 可以生成评论呢
    oFile.close();

    publicclass PropertyTest {

    public static void main(String[] args) throws FileNotFoundException, IOException {

    // TODO Auto-generated method stub

    Properties pro=new Properties();

    FileInputStream f=new FileInputStream("config.properties");

    pro.load(f);

    // Set<String> stringPropertyNames()     

    //返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。

    Iterator<String> s=pro.stringPropertyNames().iterator();

    while(s.hasNext()){

    String key=s.next();

    System.out.println(key +"  "+pro.getProperty(key));

    }

    f.close();

    //保存Properties文件 写到输出流中去了

    FileOutputStream oFile=new FileOutputStream("b.properties",true);//表示文件可追加呢

    pro.setProperty("phone", "10086");

    pro.store(oFile,"The new Properties");//生成评论了

    oFile.close();

    }

  • 相关阅读:
    Go 接口
    Go 参数传递
    Go 结构体
    Go 指针
    使用ContentType处理大量的外键关系
    django的render的特殊用法
    restframework中的那些参数你知道吗?
    scrapy框架
    numpy如何使用
    HTML 5 audio标签
  • 原文地址:https://www.cnblogs.com/lixiaowei395659729/p/6885720.html
Copyright © 2011-2022 走看看