Spring项目读取resource下的文件

目录

  一、前提条件

  二、使用ClassPathResource类读取

    2.1、Controller、service中使用ClassPathResource

    2.2、单元测试使用ClassPathResource

  三、使用FileSystemResource类读取文件

  

 

一、前提条件

  要去读取的文件是存放在project/src/main/resources目录下的,如下图中的test.txt文件。

二、使用ClassPathResource类读取

2.1、Controller、service中使用ClassPathResource

  不管是在哪一层(service、controller..),都可以使用这种方式,甚至是单元测试中,也是可以的。

package cn.ganlixin.demo.controller;
 
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
@RestController
public class TestController {
 
    @RequestMapping("testFile")
    public String testFile() throws IOException {
        // ClassPathResource类的构造方法接收路径名称,自动去classpath路径下找文件
        ClassPathResource classPathResource = new ClassPathResource("test.txt");
         
        // 获得File对象,当然也可以获取输入流对象
        File file = classPathResource.getFile();
         
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line);
        }
         
        return content.toString();
    }
}

2.2、单元测试使用ClassPathResource

单元测试也是可以使用ClassPathResource,但是需要注意:

  1、单元测试的资源目录默认是project/src/test/resources,如果该目录下找到单元测试需要的文件,那么就用找到的文件;

  2、如果在单元测试的资源目录下没有找到单元测试需要的文件,就会去找/project/src/main/resources目录下的同名文件进行操作。

package cn.ganlixin.demo.example;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationConfigTest {
 
    @Test
    public void testFile() throws IOException {
        final ClassPathResource classPathResource = new ClassPathResource("test.txt");
        final File file = classPathResource.getFile();
 
        final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

三、使用FileSystemResource类读取文件

  FileSystemResource这个类在找文件的时候就是按照给定的路径名去找,默认的当前目录就是项目根目录。

  使用该类来查找文件时,需要保证文件路径完全正确,另外,在代码中将路径写死是一个不好的习惯,特别是一个文件的路径在不同的主机上的位置(层级目录)不一定相同,所以我们开发过程中很少使用这种方式。

  FileSystemResource的用法和ClassPathResource的用法相似,因为他们都继承了AbstractResource这个抽象类。

package cn.ganlixin.demo.example;
 
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class FileTest extends ApplicationConfigTest {
 
    @Test
    public void testFile() throws IOException {
 
        FileSystemResource resource = new FileSystemResource("./");
        System.out.println(resource.getFile().getAbsolutePath());
        // 传入当前路径,获得的是项目根目录:/Users/ganlixin/code/Spring/demo/example/.
 
        // 传入根目录路径,获得的就是操作系统的根目录
        resource = new FileSystemResource("/");
        System.out.println(resource.getFile().getAbsolutePath());  // 输出 /
 
        // 获取单元测试resources目录下的test.txt,需要指定详细的路径
        resource = new FileSystemResource("src/test/resources/test.txt");
        final File file = resource.getFile();
 
        final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

  这里就列举了两种方式,还有其他很多方式,这两种应该够用了

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过`ResourceLoader`接口来读取resource文件。可以使用`@Autowired`注入ResourceLoader对象,然后使用`getResource()`方法来获取resource文件的URL或InputStream。 以下是一个示例代码,演示如何读取resource文件: ```java import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @Component public class ResourceReader { private final ResourceLoader resourceLoader; public ResourceReader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @PostConstruct public void readResource() throws IOException { Resource resource = resourceLoader.getResource("classpath:example.txt"); InputStream inputStream = resource.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 在上面的代码中,我们使用`ResourceLoader`接口来获取resource文件的URL或InputStream,并使用BufferedReader来读取文件内容。在这个例子中,我们读取了一个名为`example.txt`的文件,它位于classpath下的`resources`文件夹中。如果您想读取其他文件,只需要更改`getResource()`方法中的文件路径即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值