av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

java獲取resource目錄路徑

在Java中,獲取resource文件路徑的方法有以下幾種:

1、使用類加載器獲取資源文件路徑

2、使用ClassPathResource獲取資源文件路徑

3、使用FileSystemResource獲取資源文件路徑

4、使用URL獲取資源文件路徑

5、使用相對(duì)路徑獲取資源文件路徑

下面分別詳細(xì)介紹這幾種方法。

1、使用類加載器獲取資源文件路徑

public String getResourcePathByClassLoader() {
    ClassLoader classLoader = getClass().getClassLoader();
    URL resourceUrl = classLoader.getResource("test.txt");
    return resourceUrl.getPath();
}

2、使用ClassPathResource獲取資源文件路徑

import org.springframework.core.io.ClassPathResource;
public String getResourcePathByClassPathResource() {
    ClassPathResource classPathResource = new ClassPathResource("test.txt");
    return classPathResource.getFile().getAbsolutePath();
}

3、使用FileSystemResource獲取資源文件路徑

import org.springframework.core.io.FileSystemResource;
import java.io.File;
public String getResourcePathByFileSystemResource() {
    FileSystemResource fileSystemResource = new FileSystemResource(new File("test.txt"));
    return fileSystemResource.getFile().getAbsolutePath();
}

4、使用URL獲取資源文件路徑

public String getResourcePathByURL() throws MalformedURLException {
    URL resourceUrl = getClass().getClassLoader().getResource("test.txt");
    return resourceUrl.getPath();
}

5、使用相對(duì)路徑獲取資源文件路徑

public String getResourcePathByRelativePath() {
    return getClass().getClassLoader().getResource("test.txt").getPath();
}

以上就是Java中獲取resource文件路徑的幾種方法,在實(shí)際開發(fā)中,可以根據(jù)需要選擇合適的方法來獲取資源文件路徑,需要注意的是,這些方法都是基于類加載器的,所以在使用時(shí)需要確保資源文件已經(jīng)被加載到類加載器中,如果資源文件沒有被加載,那么這些方法將返回null。


本文名稱:java獲取resource目錄路徑
URL網(wǎng)址:http://uogjgqi.cn/article/cdppeci.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流