詳解Spring boot操作文件的多種方式
一、獲取文件路徑
獲取文件路徑
1、class.getResource(path)
其中的參數(shù)path有兩種形式,一種是以“/”開(kāi)頭的,另一種是不以"/"開(kāi)頭;
- 「以'/'開(kāi)頭的表示」:從項(xiàng)目的根路徑下去獲取文件即classPath目錄下。
- 不以"/"開(kāi)頭:以該類(lèi)對(duì)象所在位置**為根路徑來(lái)進(jìn)行查找的。
// 1.獲取當(dāng)前文件所在的路徑
System.out.println(this.getClass().getResource("").getPath());
// 2.獲取再 target 下 classpath 路徑
System.out.println(this.getClass().getResource("/").getPath());

class.getResource()和class.getResourceAsStream()方式的使用在路徑上是一致的。
2、ClassLoader.getResource(path)
// 3.也是獲取 classpath 的絕對(duì)路徑
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
// 4.也是獲取 classpath 的絕對(duì)路徑
System.out.println(this.getClass().getClassLoader().getResource("").getPath());
// 5.也是獲取 classpath 的絕對(duì)路徑
System.out.println(ClassLoader.getSystemResource("").getPath());

3、項(xiàng)目路徑
//6.獲取當(dāng)前項(xiàng)目路徑(此方法與 7 效果相同,但是可以將路徑轉(zhuǎn)為標(biāo)準(zhǔn)形式,會(huì)處理"."和"..")
System.out.println(new File("").getCanonicalPath());
// 7.獲取項(xiàng)目絕對(duì)路徑(不會(huì)處理"."和"..")
System.out.println(new File("").getAbsolutePath());
//8.user.dir
System.out.println(System.getProperty("user.dir"));

二、操作文件的三種方式
1、ClassPath
讀取resources下配置文件【文件只能為Properties、xml、JSON】
//讀取 ClassPath 下的文件信息
//1、類(lèi)加載器
InputStream resourceAsStream = DaoFactory.class.getClassLoader().getResourceAsStream("data.properties");
//2、當(dāng)前線程加載器
lassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream(path);
2、FileSystem
指定文件路徑的方式讀取文件信息,讀取resources下static文件夾中的文件
String path = Thread.currentThread().getContextClassLoader().getResource("static/internal.csv").getPath();
File file = new File(path);
3、UrlResource
通過(guò) HTTP 的方式讀取云服務(wù)的文件,我們也可以把配置文件放到 GitHub 或者 Gitee 上。
URLConnection con = this.url.openConnection(); InputStream inputStream = con.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content);
到此這篇關(guān)于Spring boot操作文件的幾種方式的文章就介紹到這了,更多相關(guān)Spring boot操作文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
IDEA創(chuàng)建yml文件不顯示小樹(shù)葉創(chuàng)建失敗問(wèn)題的解決方法
這篇文章主要介紹了IDEA創(chuàng)建yml文件不顯示小樹(shù)葉創(chuàng)建失敗問(wèn)題的解決方法,需要的朋友可以參考下2020-07-07
Springboot熱部署實(shí)現(xiàn)原理及實(shí)例詳解
這篇文章主要介紹了Springboot熱部署實(shí)現(xiàn)原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Eclipse中導(dǎo)入Maven Web項(xiàng)目并配置其在Tomcat中運(yùn)行圖文詳解
這篇文章主要介紹了Eclipse中導(dǎo)入Maven Web項(xiàng)目并配置其在Tomcat中運(yùn)行圖文詳解,需要的朋友可以參考下2017-12-12
springboot自動(dòng)重連Redis的實(shí)現(xiàn)方法
由于網(wǎng)絡(luò)或服務(wù)器問(wèn)題,Redis連接可能會(huì)斷開(kāi),導(dǎo)致應(yīng)用程序無(wú)法繼續(xù)正常工作,本文主要介紹了springboot自動(dòng)重連Redis的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Can''t use Subversion command line client:svn 報(bào)錯(cuò)處理
這篇文章主要介紹了Can't use Subversion command line client:svn 報(bào)錯(cuò)處理的相關(guān)資料,需要的朋友可以參考下2016-09-09
SpringBoot開(kāi)發(fā)中的組件和容器詳解
這篇文章主要介紹了SpringBoot開(kāi)發(fā)中的組件和容器詳解,SpringBoot 提供了一個(gè)內(nèi)嵌的 Tomcat 容器作為默認(rèn)的 Web 容器,同時(shí)還支持其他 Web 容器和應(yīng)用服務(wù)器,需要的朋友可以參考下2023-09-09
Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程
這篇文章主要介紹了Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

