歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> IntelliJ IDEA讀取資源文件

IntelliJ IDEA讀取資源文件

日期:2017/3/1 9:33:25   编辑:Linux編程

官方文檔

以下是jetbrain官網對idea中資源文件的解釋,文章最後有此文的鏈接。

這裡的資源文件包括properties文件、圖片、dtd文件、xml文件。這些文件被放在項目的classpath路徑下。通常可以使用以下方法來獲取他們:

ResourceBundle.getBundle() //獲取properties文件或者資源包(resource bundles)
loadResourceAsStream() //獲取icon或者其他文件

build應用時,idea會復制這些資源文件到應用的輸出目錄,並且保持這些資源的目錄結構。Idea默認的資源文件擴展名有properties、xml、html、dtd、tld、gif、png、jpeg、jpg。你可以在complile對話框裡用正則表達式的形式來配置idea默認的資源文件擴展名,進而來實現自定義資源文件的擴展名。

實例

創建一個projec,結構如下:

如你所見,res是一個資源目錄,創建資源文件目錄的方式是

1.新建一個普通目錄命名為res

2.項目屬性對話框->選擇res目錄,點擊res按鈕,如下圖:

關鍵代碼為:

URL url = TestFrame.class.getResource(“/white.jpg”);
ImageIcon icon = new ImageIcon(url);

運行結果如下

資源

https://www.jetbrains.com/idea/help/resource-files.html

IntelliJ IDEA: 字體美化(anti-aliased)和字體添加 http://www.linuxidc.com/Linux/2013-11/92609.htm

IntelliJ IDEA 12 創建Web項目圖文詳細教程 http://www.linuxidc.com/Linux/2013-05/84213.htm

用IntelliJ IDEA開發Android程序圖文教程 http://www.linuxidc.com/Linux/2013-03/81471.htm

IntelliJ IDEA 12開發haXe NME應用配置指南 http://www.linuxidc.com/Linux/2013-01/77227.htm

IntelliJ IDEA運行Play Framework的test mode http://www.linuxidc.com/Linux/2013-07/87694.htm

Ubuntu 13.04 安裝IntelliJ IDEA 12 http://www.linuxidc.com/Linux/2013-11/93014.htm

IntelliJ IDEA 12創建Maven管理的Java Web項目(圖解) http://www.linuxidc.com/Linux/2014-04/99687p2.htm

IntelliJ IDEA 的詳細介紹:請點這裡
IntelliJ IDEA 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved