項目開發中,經常會有一些靜态資源,被放置在resources目錄下,随項目打包在一起,代碼中要使用的時候,通過檔案讀取的方式,加載并使用;
本文中彙總整理了九種方式擷取resources目錄下檔案的方法。
其中公用的列印檔案方法如下:
/**
* 根據檔案路徑讀取檔案内容
*
* @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解碼。
/**
* 直接通過檔案名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");//如果路徑中帶有中文會被URLEncoder,是以這裡需要解碼
System.out.println(filePath);
getFileContent(filePath);
}
方式三
直接通過檔案名+getFile()來擷取檔案。如果是檔案路徑的話getFile和getPath效果是一樣的,如果是URL路徑的話getPath是帶有參數的路徑。
如下所示:
url.getFile()=/pub/files/foobar.txt?id=123456
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");//如果路徑中帶有中文會被URLEncoder,是以這裡需要解碼
System.out.println(filePath);
getFileContent(filePath);
}
方式四(重要)
直接使用getResourceAsStream方法擷取流,上面的幾種方式都需要擷取檔案路徑,但是在SpringBoot中所有檔案都在jar包中,沒有一個實際的路徑,是以可以使用以下方式。
/**
* 直接使用getResourceAsStream方法擷取流
* springboot項目中需要使用此種方法,因為jar包中沒有一個實際的路徑存放檔案
*
* @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包中,沒有一個實際的路徑,是以可以使用以下方式。
/**
* 直接使用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類擷取檔案流,SpringBoot中所有檔案都在jar包中,沒有一個實際的路徑,是以可以使用以下方式。
/**
* 通過ClassPathResource類擷取,建議SpringBoot中使用
* springboot項目中需要使用此種方法,因為jar包中沒有一個實際的路徑存放檔案
*
* @param fileName
* @throws IOException
*/
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}
方式七
通過絕對路徑擷取項目中檔案的位置,隻是本地絕對路徑,不能用于伺服器擷取。
/**
* 通過絕對路徑擷取項目中檔案的位置(不能用于伺服器)
* @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("")擷取目前的絕對路徑,隻是本地絕對路徑,不能用于伺服器擷取。
/**
* 通過絕對路徑擷取項目中檔案的位置(不能用于伺服器)
* @param fileName
* @throws IOException
*/
public void function8(String fileName) throws IOException {
//參數為空
File directory = new File("");
//規範路徑:getCanonicalPath() 方法傳回絕對路徑,會把 ..\ 、.\ 這樣的符号解析掉
String rootCanonicalPath = directory.getCanonicalPath();
//絕對路徑:getAbsolutePath() 方法傳回檔案的絕對路徑,如果構造的時候是全路徑就直接傳回全路徑,如果構造時是相對路徑,就傳回目前目錄的路徑 + 構造 File 對象時的路徑
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);
}
方式九
主要是通過設定環境變量,将檔案放在環境變量中,原理也是通過絕對路徑擷取。
示例中我設定了一個環境變量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-example
System.getenv("TEST_ROOT");
System.getProperty("TEST_ROOT")
通過設定環境變量的方式,然後通過絕對路徑擷取檔案
/**
* 通過絕對路徑擷取項目中檔案的位置
*
* @param fileName
* @throws IOException
*/
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","E:\\WorkSpace\\Git\\spring-framework-learning-example");
//參數為空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
版權聲明:本文為CSDN部落客「leo825...」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。原文連結:https://blog.csdn.net/u011047968/article/details/107311462