日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
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í)際開(kāi)發(fā)中,可以根據(jù)需要選擇合適的方法來(lái)獲取資源文件路徑,需要注意的是,這些方法都是基于類加載器的,所以在使用時(shí)需要確保資源文件已經(jīng)被加載到類加載器中,如果資源文件沒(méi)有被加載,那么這些方法將返回null。


文章名稱:java獲取resource目錄路徑
分享路徑:http://www.5511xx.com/article/cdppeci.html