Java讀取resources目錄下文件路徑的九種代碼示例教程
前言
資源管理是開發(fā)過程中不可或缺的一部分。資源文件,如配置文件、圖片和文本文件,通常被放置在項(xiàng)目的resources目錄下,以便于管理和訪問。然而,對于初學(xué)者來說,如何正確地讀取這些文件路徑可能會(huì)遇到一些困惑。本文將深入探討Java中讀取resources目錄下文件路徑的幾種常見方法,幫助開發(fā)者更有效地管理項(xiàng)目資源,并確保應(yīng)用程序的健壯性和可維護(hù)性。
代碼一:根據(jù)文件路徑讀取文件內(nèi)容
/**
* 根據(jù)文件路徑讀取文件內(nèi)容
* @param fileInPath
* @throws IOException
*/
public static void getFileContent(Object fileInPath) throws IOException {
BufferedReader br = null;
if (fileInPath == null) {
return;
}
if (fileInPath instanceof String) {
br = new BufferedReader(new FileReader(new File((String) fileInPath)));
} else if (fileInPath instanceof InputStream) {
br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
}
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
代碼二:使用getResource和getPath方法
這里的getResource(“”)里面是空字符串
public void function1(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath();
//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = path + fileName;
System.out.println(filePath);
getFileContent(filePath);
}
代碼三:使用getResource和getPath方法
直接通過getResource(fileName)方法獲取文件路徑,如果路徑中帶有中文要使用URLDecoder.decode進(jìn)行解碼。
/**
* 直接通過文件名getPath來獲取路徑
* @param fileName
* @throws IOException
*/
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路徑中帶有中文會(huì)被URLEncoder,因此這里需要解碼
System.out.println(filePath);
getFileContent(filePath);
}
代碼四:通過文件名+getFile()來獲取文件
文件路徑的話getFile和getPath效果是一樣的,如果是URL路徑的話getPath是帶有參數(shù)的路徑。如下所示:
url.getFile()=/pub/files/foobar.txt?id=zhouzhou url.getPath()=/pub/files/foobar.txt
使用getFile()方式獲取文件的代碼如下:
/**
* 直接通過文件名+getFile()來獲取
* @param fileName
* @throws IOException
*/
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路徑中帶有中文會(huì)被URLEncoder,因此這里需要解碼
System.out.println(filePath);
getFileContent(filePath);
}
代碼五:使用getResourceAsStream方法獲取流(無需文件路徑)
SpringBoot中所有文件都在jar包中,沒有一個(gè)實(shí)際的路徑,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法獲取流
* springboot項(xiàng)目中需要使用此種方法,因?yàn)閖ar包中沒有一個(gè)實(shí)際的路徑存放文件
* @param fileName
* @throws IOException
*/
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}
使用getResourceAsStream方法獲取流,不使用getClassLoader可以使用getResourceAsStream(“/配置測試.txt”)直接從resources根路徑下獲取,SpringBoot中所有文件都在jar包中,沒有一個(gè)實(shí)際的路徑,因此可以使用以下方式。
/**
* 直接使用getResourceAsStream方法獲取流
* 如果不使用getClassLoader,可以使用getResourceAsStream("/配置測試.txt")直接從resources根路徑下獲取
* @param fileName
* @throws IOException
*/
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}
代碼六:通過ClassPathResource類獲取文件流
/**
* 通過ClassPathResource類獲取,建議SpringBoot中使用
* springboot項(xiàng)目中需要使用此種方法,因?yàn)閖ar包中沒有一個(gè)實(shí)際的路徑存放文件
* @param fileName
* @throws IOException
*/
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}
代碼七:通過絕對路徑獲取項(xiàng)目中文件的位置
/**
* 通過絕對路徑獲取項(xiàng)目中文件的位置(不能用于服務(wù)器)
* @param fileName
* @throws IOException
*/
public void function7(String fileName) throws IOException {
String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Git\spring-framework-learning-example
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
代碼八:new File(“”)獲取當(dāng)前的絕對路徑
/**
* 通過絕對路徑獲取項(xiàng)目中文件的位置(不能用于服務(wù)器)
* @param fileName
* @throws IOException
*/
public void function8(String fileName) throws IOException {
//參數(shù)為空
File directory = new File("");
//規(guī)范路徑:getCanonicalPath() 方法返回絕對路徑,會(huì)把 ..\ 、.\ 這樣的符號解析掉
String rootCanonicalPath = directory.getCanonicalPath();
//絕對路徑:getAbsolutePath() 方法返回文件的絕對路徑,如果構(gòu)造的時(shí)候是全路徑就直接返回全路徑,如果構(gòu)造時(shí)是相對路徑,就返回當(dāng)前目錄的路徑 + 構(gòu)造 File 對象時(shí)的路徑
String rootAbsolutePath =directory.getAbsolutePath();
System.out.println(rootCanonicalPath);
System.out.println(rootAbsolutePath);
String filePath = rootCanonicalPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
代碼九:設(shè)置環(huán)境變量
主要是通過設(shè)置環(huán)境變量,將文件放在環(huán)境變量中,原理也是通過絕對路徑獲取。
設(shè)置一個(gè)環(huán)境變量:TEST_ROOT=D:\WorkSpace\Git\spring-framework-learning-example
System.getenv("TEST_ROOT");
System.getProperty("TEST_ROOT")
通過設(shè)置環(huán)境變量的方式,然后通過絕對路徑獲取文件
/**
* 通過絕對路徑獲取項(xiàng)目中文件的位置
* @param fileName
* @throws IOException
*/
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");
//參數(shù)為空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
總結(jié)
到此這篇關(guān)于Java讀取resources目錄下文件路徑的九種代碼示例的文章就介紹到這了,更多相關(guān)Java讀取resources目錄下文件路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea自定義TODO標(biāo)識(shí)實(shí)現(xiàn)方式
文章介紹了如何在代碼中使用TODO標(biāo)簽,包括添加標(biāo)簽、修改標(biāo)簽顏色、追加Filter過濾器等步驟,并介紹了使用LiveTemplates快速添加TODO標(biāo)簽的方法,以提高代碼的可讀性和維護(hù)性2026-04-04
Mybatis批量插入返回成功的數(shù)目實(shí)例
這篇文章主要介紹了Mybatis批量插入返回成功的數(shù)目實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
idea的language?level默認(rèn)為5的解決方案
文章介紹了在IDEA中修改項(xiàng)目語言級別(language?level)時(shí)遇到的問題,以及解決方法,方法1是通過在父pom文件中添加配置來解決,方法2是修改maven的settings.xml文件2025-12-12
Java解析JSON數(shù)據(jù)時(shí)報(bào)錯(cuò)問題解決方案
這篇文章主要介紹了Java解析JSON數(shù)據(jù)時(shí)報(bào)錯(cuò)問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
java實(shí)現(xiàn)Google郵箱SMTP協(xié)議的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用java實(shí)現(xiàn)Google郵箱SMTP協(xié)議,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06

