/**.
 */

package com.encdata.circles;

import java.io.*;
import java.util.Iterator;
import java.util.Properties;

/**.
 *
 * @author Devil
 *
 */
public class testGetProperties {
  
  public static void main(String [] args) throws IOException{
    readProperties();
    readProperties2();
    writeProperties();
}

  /**
   * 读取properties属性文件
   */
  public static void readProperties()throws IOException{
    
    Properties prop=new Properties();
    
    InputStream inStream = testGetProperties.class.getClassLoader().getResourceAsStream("config/databus.properties");  
    
    /*FileInputStream inputFile=new FileInputStream("classpath:config/server.properties");*/
    prop.load(inStream);
    prop.setProperty("port", "3307");
    
    Iterator<String> it=prop.stringPropertyNames().iterator();
    while(it.hasNext()){
        String key=it.next();
        System.out.println(key+":"+prop.getProperty(key));
        
    }
    
    
    System.out.println(prop.getProperty("port"));
    
    inStream.close();
  }
  
  /**
   * 读取properties属性文件
   */
  public static void readProperties2()throws IOException{
    
    Properties prop=new Properties();
    
    InputStream inStream = testGetProperties.class.getClassLoader().getResourceAsStream("config/databus.properties");  
    
    /*FileInputStream inputFile=new FileInputStream("classpath:config/server.properties");*/
    prop.load(inStream);
    
    Iterator<String> it=prop.stringPropertyNames().iterator();
    while(it.hasNext()){
        String key=it.next();
        System.out.println(key+":"+prop.getProperty(key));
        
    }
    
    /*prop.setProperty("port", "3307");
    System.out.println(prop.getProperty("port"));*/
    
    inStream.close();
  }

/**
 * 生成properties属性文件
 */
public static void writeProperties()  {

    Properties prop=new Properties();
    try{
        FileOutputStream oFile=new FileOutputStream(new File("D://sys-config.properties"),true);
        prop.setProperty("driver-name","oracle.jdbc.driver.OracleDriver");
        prop.setProperty("url","jdbc:oracle:thin:@localhost:1521:ORCL");
        prop.setProperty("user-name","drp1");
        prop.setProperty("password","drp1");
        prop.store(oFile,"sys-config");
        oFile.close();
    } catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    }
}
  
  

}