Java或者JAR包擷取讀取資源檔的路徑的問題總結

來源:互聯網
上載者:User

這裡分為具體兩種:

第一種:資源檔為一般尾碼檔案

第二種:資源檔為圖片檔案

【NO1】第一種

  使用這行代碼可以擷取class類的根目錄的路徑

  String path =Thread.currentThread().getContextClassLoader().getResource("").getPath();

 例子:我用的開發軟體MyEclipse 6.5

 假設專案檔夾如下:

   files———bin——core(產生class包)

       |               |         |——Main.class(產生的class檔案)

       |               |

       |               |——resource( 產生資源檔夾)

       |                         |——a.bat

       |                         |——b.png

       |———src——core(源包)  

                   |             |—— Main.java(原始碼)

                   |

                   |——resource(源資源檔夾) 

                                 |——a.bat

                                 |——b.png

//原始碼Main.java

//============================================================

package core;
import java.io.File;

public class Main {

    public static void main(String[] args){
        try{
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();    //添加
        File af=new File(path+"/resource");
        if(!af.exists()) System.out.println("nullEXIST");;
        String[] files =af.list();
        if(files.length==0) System.out.println("nullLENGTH");;
        for(int i=0;i<files.length;i++){
            if(files[i]!=null)System.out.println(files[i]);
            else System.out.println("null");
        }
        
        }
        catch(Exception e){
            System.out.println("HugeERROR");
        }
    }
        
}

// ===============================================================

    運行結果:

 a.bat

 b.png   

 就對了   

【NO2】第二種

 建議使用下面( 2 )方法,因為(1)jar之後可能出現找不到的問題 (之前我試過出現這種情況)

 這裡代碼省了

(1)、你將所有資源打包為epm.jar,你的類位於一個包中:package core;你的圖片資源全部放在images檔案夾中,而images檔案夾也位於core包內。這樣的話,最終的路徑表現為:
epm———bin——core(產生class包)

       |               |         |——Main.class(產生的class檔案)

       |               |

       |               |——images( 產生資源檔夾)

       |                         |——system.bat

       |                         |——background.png

       |———src——core(源包)  

                   |             |—— Main.java(原始碼)

                   |

                   |——images(源資源檔夾) 

                                 |——system.bat

                                 |——background.png

 

  可以通過相對路徑來訪問:       

  java.net.URL imUrl = getClass().getResource("images/background.png"); 
  ImageIcon im = new ImageIcon(imUrl);

(2)、另一種情況,如果你的類有很多,並且包的結構很複雜,應該把圖片放到最外層,讓所有的類通過絕對路徑來訪問該圖片

epm———bin——core(產生class包)

       |               |         |——Main.class(產生的class檔案)

       |               |

       |               |——images( 產生資源檔夾)

       |                         |——system.bat

       |                         |——background.png

       |———src——core(源包)  

                   |             |—— Main.java(原始碼)

                   |

                   |——images(源資源檔夾) 

                                 |——system.bat

                                 |——background.png

java.net.URL imUrl = getClass().getResource("/images/background.png");
ImageIcon im = new ImageIcon(imgUrl);

區別非常細微,僅僅是在“images”的前面加了一個反斜線"/",這個反斜線就表示根目錄,沒有反斜線就表示相對路徑。

這些都是細心的問題了...... 

聯繫我們

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