java讀取資源路徑的幾種實(shí)現(xiàn)方式
java讀取資源路徑幾種方式
@Test public void path() throws IOException { System.out.println("用戶當(dāng)前工作目錄"+System.getProperty("user.dir")); File directory = new File(""); String path2 = directory.getCanonicalPath(); System.out.println("當(dāng)前工作目錄1:"+path2); String path3 = directory.getAbsolutePath(); System.out.println("當(dāng)前工作目錄2:"+path3); String path = ClassUtils.getDefaultClassLoader().getResource("").getPath(); System.out.println("類加載器返回默認(rèn)路徑:"+path); String path1 = ResourceUtils.getURL("classpath:").getPath(); System.out.println("ResourceUtils返回默認(rèn)路徑:"+path1); String resourcePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); System.out.println("resourcePath返回默認(rèn)路徑:"+resourcePath); ClassPathResource classPathResource = new ClassPathResource("excel/xx.xlsx"); System.out.println("ClassPathResource返回資源路徑:"+classPathResource.getURL()); URL resource = this.getClass().getClassLoader().getResource("excel/xx.xlsx"); System.out.println("類加載器返回資源路徑:"+resource.getPath()); URL url = ResourceUtil.getResource("excel/xx.xlsx"); System.out.println("ResourceUtil返回資源路徑:"+url.getPath()); }
注意:
以上是在Junit測(cè)試文件中的結(jié)果
工作可以精確到所在模塊,而普通類里打印是只有主目錄沒(méi)有模塊的。
如下:
public static void main(String[] args) throws IOException { System.out.println("用戶當(dāng)前工作目錄"+System.getProperty("user.dir")); File directory = new File(""); String path2 = directory.getCanonicalPath(); System.out.println("當(dāng)前工作目錄1:"+path2); String path3 = directory.getAbsolutePath(); System.out.println("當(dāng)前工作目錄2:"+path3); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java開(kāi)發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗(yàn)方式
這篇文章主要介紹了java開(kāi)發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗(yàn)方式,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下2021-09-09SpringBoot自定義轉(zhuǎn)換器應(yīng)用實(shí)例講解
SpringBoot在響應(yīng)客戶端請(qǐng)求時(shí),將提交的數(shù)據(jù)封裝成對(duì)象時(shí),使用了內(nèi)置的轉(zhuǎn)換器,SpringBoot 也支持自定義轉(zhuǎn)換器,這個(gè)內(nèi)置轉(zhuǎn)換器在 debug的時(shí)候,可以看到,提供了124個(gè)內(nèi)置轉(zhuǎn)換器2022-08-08Java中的封裝性(包含this關(guān)鍵字,構(gòu)造器等)
這篇文章主要介紹了Java中的封裝性(包含this關(guān)鍵字,構(gòu)造器等)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Java將一個(gè)正整數(shù)分解質(zhì)因數(shù)的代碼
這篇文章主要介紹了將一個(gè)正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5,需要的朋友可以參考下2017-02-02Spring MessageSource獲取消息不符合預(yù)期的問(wèn)題解決方案
最近我參與的產(chǎn)品要做國(guó)際化支持,選擇了用Spring MessageSource來(lái)實(shí)現(xiàn),這個(gè)Spring 框架提供的工具使用很簡(jiǎn)單,網(wǎng)上有各種教程文章,這里不做贅述,只說(shuō)一個(gè)實(shí)際遇到的問(wèn)題及解決方案,需要的朋友可以參考下2024-01-01springboot多節(jié)點(diǎn)應(yīng)用里的雪花算法唯一性詳解
雪花算法在單節(jié)點(diǎn)下唯一,但在多副本Kubernetes環(huán)境中可能重復(fù),通過(guò)修改Pod名稱生成workId,解決了這個(gè)問(wèn)題,同時(shí)避免了第三方組件和網(wǎng)絡(luò)請(qǐng)求,本文給大家介紹springboot多節(jié)點(diǎn)應(yīng)用里的雪花算法唯一性,感興趣的朋友一起看看吧2024-12-12SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn),我們知道SpringBoot自己在“后臺(tái)”幫我們配置了很多原本需要我們手動(dòng)去的東西,至于這個(gè)“后臺(tái)”是啥,就是Starter機(jī)制2022-09-09