最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot獲取resources目錄下的文件

 更新時(shí)間:2024年10月31日 11:00:18   作者:pan_junbiao  
在 Spring Boot 項(xiàng)目中,獲取 resources 目錄中的文件路徑通常涉及到訪問類路徑資源,Spring Boot 提供了一些工具類和方法,下面小編就來和大家詳細(xì)聊聊

在 Spring Boot 項(xiàng)目中,獲取 resources 目錄中的文件路徑通常涉及到訪問類路徑資源(classpath resources)。Spring Boot 提供了一些工具類和方法,可以方便地訪問這些資源。以下是一些常見的方法:

首先,我們?cè)?Spring Boot 項(xiàng)目中的 resources (資源文件目錄)下創(chuàng)建 file 目錄,然后在 file 目錄下創(chuàng)建 myBlog.txt 文件,并在文件中輸入內(nèi)容:您好,歡迎訪問 pan_junbiao的博客。

1、使用項(xiàng)目路徑

使用字符串方式寫入文件的項(xiàng)目路徑,這是最簡(jiǎn)單的方式。

String path = "src/main/resources/file/myBlog.txt";
@Test
public void readFileByPath() throws IOException
{
    String path = "src/main/resources/file/myBlog.txt";
    File file = new File(path);
 
    if (file.exists())
    {
        try (BufferedReader reader = new BufferedReader(new FileReader(file)))
        {
            String line;
            while ((line = reader.readLine()) != null)
            {
                System.out.println(line);
            }
        }
    } else
    {
        System.out.println("未找到文件!");
    }
}

執(zhí)行結(jié)果:

2、使用 ApplicationContext 接口

ApplicationContext 是 Spring 框架中的一個(gè)核心接口,它是 Spring IoC 容器的實(shí)現(xiàn)之一,用于管理和組織應(yīng)用程序中的各種 Bean,同時(shí)提供了一系列功能來支持依賴注入、AOP 等特性。同時(shí) ApplicationContext 提供了對(duì)資源的訪問能力,如文件、URL等。這通過 Resource 接口和 ResourceLoader 接口實(shí)現(xiàn),使得訪問外部資源變得簡(jiǎn)單。

@Autowired
private ApplicationContext applicationContext;
 
@Test
public void readResourceFile() throws IOException
{
    Resource resource = applicationContext.getResource("classpath:/file/myBlog.txt");
    InputStream inputStream = resource.getInputStream();
 
    if (inputStream != null) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
    } else {
        System.out.println("File not found");
    }
}

3、使用 ResourceLoader 接口

ResourceLoader 是 Spring 提供的一個(gè)接口,用于加載資源。你可以在 Spring Bean 中注入 ResourceLoader,然后使用它來加載資源。

@Autowired
private ResourceLoader resourceLoader;
 
@Test
public void readFileByResourceLoader() throws IOException
{
    Resource resource = resourceLoader.getResource("classpath:/file/myBlog.txt");
    if (resource.exists())
    {
        Path path = Paths.get(resource.getURI());
        String content = new String(Files.readAllBytes(path));
        System.out.println(content);
    } else
    {
        System.out.println("未找到文件!");
    }
}

4、使用 ClassLoader 類

你也可以使用當(dāng)前類的 ClassLoader 來加載資源。ClassLoader 的 getResource 和 getResourceAsStream 方法可以訪問類路徑資源。

@Test
public void readFileByClassLoader() throws IOException
{
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream("file/myBlog.txt");
 
    if (inputStream != null)
    {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)))
        {
            String line;
            while ((line = reader.readLine()) != null)
            {
                System.out.println(line);
            }
        }
    } else
    {
        System.out.println("未找到文件!");
    }
}

5、使用 PathMatchingResourcePatternResolver 類

PathMatchingResourcePatternResolver 是 Spring 提供的一個(gè)工具類,用于解析資源路徑模式。它擴(kuò)展了 ResourceLoader 的功能。

@Test
public void readFileByResourcePattern() throws IOException
{
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource resource = resolver.getResource("classpath:/file/myBlog.txt");
    if (resource.exists())
    {
        Path path = resource.getFile().toPath();
        String content = new String(Files.readAllBytes(path));
        System.out.println(content);
    } else
    {
        System.out.println("未找到文件!");
    }
}

注意:resource.getFile() 方法在某些情況下可能會(huì)拋出 UnsupportedOperationException,特別是在資源是從 JAR 文件中加載時(shí)。所以,更通用的方法是使用 InputStream 來讀取文件內(nèi)容。

到此這篇關(guān)于SpringBoot獲取resources目錄下的文件的文章就介紹到這了,更多相關(guān)SpringBoot獲取resources目錄下文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

奉化市| 桃江县| 庆安县| 建水县| 瓮安县| 大洼县| 大连市| 木兰县| 新巴尔虎左旗| 资阳市| 札达县| 蒲城县| 澳门| 涿鹿县| 池州市| 新沂市| 全椒县| 项城市| 东阳市| 丰宁| 墨玉县| 镇宁| 呼图壁县| 景谷| 个旧市| 驻马店市| 铜陵市| 元朗区| 瑞安市| 理塘县| 天门市| 井冈山市| 措美县| 崇义县| 宽甸| 呼伦贝尔市| 松原市| 南溪县| 赤城县| 方城县| 武功县|