欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)讀取resources目錄下的文件路徑的九種方式

 更新時間:2022年04月02日 11:20:36   作者:leo825...  
本文主要介紹了Java實現(xiàn)讀取resources目錄下的文件路徑的九種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前情提要

本文中提供了九種方式獲取resources目錄下文件的方式。其中打印文件的方法如下:

? ? /**
? ? ?* 根據(jù)文件路徑讀取文件內容
? ? ?*
? ? ?* @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是帶有參數(shù)的路徑。如下所示:

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 {
        //參數(shù)為空
        File directory = new File("");
        //規(guī)范路徑: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);
    }

方式九

主要是通過設置環(huán)境變量,將文件放在環(huán)境變量中,原理也是通過絕對路徑獲取。
示例中我設置了一個環(huán)境變量:TEST_ROOT=E:\\WorkSpace\\Git\\spring-framework-learning-example

 System.getenv("TEST_ROOT");
 System.getProperty("TEST_ROOT")

通過設置環(huán)境變量的方式,然后通過絕對路徑獲取文件

    /**
     * 通過絕對路徑獲取項目中文件的位置
     *
     * @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);
    }

到此這篇關于Java實現(xiàn)讀取resources目錄下的文件路徑的九種方式的文章就介紹到這了,更多相關Java 讀取resources目錄下文件路徑內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Mybatis延遲加載原理和延遲加載配置詳解

    Mybatis延遲加載原理和延遲加載配置詳解

    這篇文章主要介紹了Mybatis延遲加載原理和延遲加載配置詳解,MyBatis中的延遲加載,也稱為懶加載,是指在進行表的關聯(lián)查詢時,按照設置延遲規(guī)則推遲對關聯(lián)對象的select查詢,需要的朋友可以參考下
    2023-10-10
  • JAVA 枚舉單例模式及源碼分析的實例詳解

    JAVA 枚舉單例模式及源碼分析的實例詳解

    這篇文章主要介紹了 JAVA 枚舉單例模式及源碼分析的實例詳解的相關資料,需要的朋友可以參考下
    2017-08-08
  • 解決工具接口調用報錯:error:Unsupported Media Type問題

    解決工具接口調用報錯:error:Unsupported Media Type問題

    當遇到"UnsupportedMediaType"錯誤時,意味著HTTP請求的Content-Type與服務器期望的不匹配,比如服務器期待接收JSON格式數(shù)據(jù),而發(fā)送了純文本格式,常見的Content-Type類型包括text/html、application/json、multipart/form-data等
    2024-10-10
  • 使用Vue+Spring Boot實現(xiàn)Excel上傳功能

    使用Vue+Spring Boot實現(xiàn)Excel上傳功能

    這篇文章主要介紹了使用Vue+Spring Boot實現(xiàn)Excel上傳,需要的朋友可以參考下
    2018-11-11
  • 使用gRPC微服務的內部通信優(yōu)化

    使用gRPC微服務的內部通信優(yōu)化

    這篇文章主要為大家介紹了微服務優(yōu)化之使用gRPC做微服務的內部通信,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-03-03
  • 老生常談 Java中的繼承(必看)

    老生常談 Java中的繼承(必看)

    下面小編就為大家?guī)硪黄仙U?Java中的繼承(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java創(chuàng)建子線程的兩種方法

    Java創(chuàng)建子線程的兩種方法

    這篇文章主要介紹了Java創(chuàng)建子線程的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • java實現(xiàn)爬蟲爬網(wǎng)站圖片的實例代碼

    java實現(xiàn)爬蟲爬網(wǎng)站圖片的實例代碼

    這篇文章主要介紹了java實現(xiàn)爬蟲爬網(wǎng)站圖片的實例代碼,需要的朋友可以參考下
    2018-06-06
  • 在springboot中使用AOP進行全局日志記錄

    在springboot中使用AOP進行全局日志記錄

    這篇文章主要介紹就在springboot中使用AOP進行全局日志記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java實現(xiàn)多選批量刪除功能(vue+Element)

    Java實現(xiàn)多選批量刪除功能(vue+Element)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)多選批量刪除功能,包括前端vue實現(xiàn)代碼文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論