java對properties檔案三種不同位置的讀取操作

來源:互聯網
上載者:User

標籤:java   properties配置   

      最近在弄項目,由於項目的特殊性出現測試環境和生產環境存在很多的參數配置發生改變,從一開始的一個個配置參數的修改的繁瑣操作到現在的設定檔的方式,大大提高了發布效率,現在將這這個進步過程中使用到的properties配置方式做了一個簡單的總結分為三種情況如下:

①這種方式是讀取在jar包外面的設定檔的路徑:

public static void main(String[] args) {  try{  InputStream in=new BufferedInputStream(new FileInputStream("resource/properties/test.properties"));  Properties p=new Properties();  p.load(in);  System.out.println("properties:"+p.getProperty("name")+","+p.getProperty("age"));  if(in!=null)  {   in.close();  }  }catch (Exception e) {   e.printStackTrace();// TODO: handle exception  }     }

②讀取在src根目錄下檔案的路徑方式,這時設定檔Config.properties不能放在src下面的包內

private GetConfig(){try{Properties prop = new Properties();     InputStream inputStream  = this.getClass().getClassLoader().getResourceAsStream("Config.properties"); prop.load(inputStream);     this.driverName = prop.getProperty("driverName","");    }catch (Exception e) {e.printStackTrace();}}

③讀取src下麵包內的設定檔

private GetConfig(){try{Properties prop = new Properties();InputStream inputStream=this.getClass().getResourceAsStream("/com/haiqi/config/Config.properties"); prop.load(inputStream);     this.driverName = prop.getProperty("driverName","");  }catch (Exception e) {e.printStackTrace();}}

沒有人理解我們。社會的價值與我們無關。我們是異樣的,因為我們是極客!

java對properties檔案三種不同位置的讀取操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.