zoukankan      html  css  js  c++  java
  • properties文件的读取和写入

    /**.
     */
    
    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);
        }
    }
      
      
    
    }
  • 相关阅读:
    动态获取Resources里面的图片列表
    在LINQ中实现多条件联合主键LEFT JOIN
    Failed to fetch URL http://dlssl.google.com/android/repository/repository.xml
    LINQ多条件OR模糊查询
    使用ILMerge将所有引用的DLL和exe文件打成一个exe文件
    安卓模拟器、电脑运行安卓系统android、apk文件
    C/C++中枚举类型(enum)
    指针详解
    Visaul Studio 2008 中配置DirectX9c的开发环境
    出自涅磐,缘自凤凰
  • 原文地址:https://www.cnblogs.com/lh-masteryi/p/9116496.html
Copyright © 2011-2022 走看看