java讀取設定檔常用的四種方式

來源:互聯網
上載者:User

標籤:mod   dao   padding   comm   屬性   position   class   keyset   推薦   

設定檔 
放置在src下面 obj.properties

className=com.store.order.dao.impl.OrderDaoImpl

方式一

@Test    public void test1() throws Exception{        //檔案放在src下面.eclipse會自動拷貝一份到bin目錄下,或者build/classes下面,        InputStream is = Class.forName("com.store.test.test").getClassLoader().getResourceAsStream("obj.properties");        Properties properties = new Properties();        properties.load(is);        String className = properties.getProperty("className");        System.out.println(className);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方式二

    /**     * 不推薦使用,檔案位置被固定了,只能在本機使用,我們把class檔案拷貝給別人就不能使用了     * @throws Exception     */    @Test    public void test2() throws Exception{        FileInputStream fis = new FileInputStream("D:\\workspaces\\store_v1.0\\src\\obj.properties");        Properties properties = new Properties();        properties.load(fis);        String className = properties.getProperty("className");        System.out.println(className);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

方式三

    @Test    public void test3() throws Exception{        //讀取設定檔,只能讀取預設尾碼名為properties(不寫)        //如果有包,寫法是com.store.obj        //細節:他只能讀取類路路徑下的檔案,不能寫入        ResourceBundle bundle = ResourceBundle.getBundle("obj");         String className = bundle.getString("className"); //通過鍵擷取值        System.out.println(className);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方式四

/**  * Spring 提供的 PropertiesLoaderUtils 允許您直接通過基於類路徑的檔案地址載入屬性資源  * 最大的好處就是:即時載入設定檔,修改後立即生效,不必重啟  */  private static void springUtil(){      Properties props = new Properties();      while(true){          try {              props=PropertiesLoaderUtils.loadAllProperties("message.properties");              for(Object key:props.keySet()){                  System.out.print(key+":");                  System.out.println(props.get(key));              }          } catch (IOException e) {              System.out.println(e.getMessage());          }          try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}      }  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

java讀取設定檔常用的四種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.