目录

  一、前提条件

  二、使用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);
}
}
}

  

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

Spring项目读取resource下的文件的更多相关文章

  1. SpringBoot项目构建成jar运行后,如何正确读取resource下的文件

    SpringBoot项目构建成jar运行后,如何正确读取resource下的文件 不管你使用的是SpringBoot 1.x还是SpringBoot2.x,在开Dev环境中使用eclipse.IEAD ...

  2. springboot项目获取resource下的文件

    package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...

  3. Spring Boot 读取 resource 下文件

    支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...

  4. Springboot项目读取resource下的静态资源方法

    如果按相对路径直接读会定位到target下,因为springboot打包后读到这里 如果做单元测试的话是找不到文件的 File jsonFile = ResourceUtils.getFile(&qu ...

  5. Maven 工程读取resource下的文件

    1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...

  6. springboot读取resource下的文件

    public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...

  7. springboot 读取 resource 下的文件

    ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...

  8. SpringBoot读取Resource下文件的几种方式(十五)

    需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...

  9. maven工程读取resource下配置文件

    maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources   下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...

随机推荐

  1. Java SpringBoot 手记

    SpringBoot Git:https://github.com/spring-projects/spring-boot Maven (mvn)环境配置: 下载地址:http://maven.apa ...

  2. Centos 7.3 镜像制作

    1.在KVM环境上准备虚拟机磁盘 [root@localhost ~]# qemu-img create -f qcow2 -o size=50G /opt/CentOS---x86_64_50G.q ...

  3. Apache JMeter系列.1

    最爱看统计 --01-- 简介 Apache JMeter Apache JMeter可用于测试静态和动态资源(文件,Servlet,Perl脚本,Java对象,数据库和查询,FTP服务器等)的性能. ...

  4. loadrunner中web_submit_data与web_submit_form区别

    以loadrunner自带的订票系统为例 当选择红框选中的模式时,所录脚本如下:回放时可以正常回放,登录成功 选另一种所录脚本如下:回放时登录不成功,session失效, 其中web_submit_f ...

  5. 关于 " i=i++"是否等价于"i=i+1"的问题探讨 -Java版/C版

    如题 (总结要点) 假设i=1 ,那么 i=i++ 的结果也就是2, 对吗? 不对,是1. -总结如下: 测试过程如下: 1.java版 public class Test { public stat ...

  6. js--同步运动json下

    这一节针对上一节讲述的bug,我们来处理一下. 这个bug存在的原因就是,一旦只要有一个属性值达到目标值就会清除定时器,所以我们要改变 的就是清除定时器的那么部分.看下面的修改 var timer; ...

  7. shell脚本攻略1

    换行符 \n echo -n 禁止换行 环境变量声明export export PATH="$PATH:/home/user/bin" 获取字符串的长度 length=${#var ...

  8. 关于Discuz x3.3页面空白解决方法

    今天找时间分析了一下,找到了页面空白的原因,可能是因为php版本兼容性的问题所致,所以只是部分用户遇到这种情况,这里分享一下.经过分析发现是sourcefunctionfunction_core.ph ...

  9. 2019年12月份关于Android Studio 需要了解的知识总结

    因为期末项目答辩的原因,我和我的小组成员一起写了个作品展示app 就是用AndroidStudio写的  具体功能呢还加上了云服务器,bmob,等等 我是不知道那个云服务器要怎么配置啊,也不会用,都是 ...

  10. java内部类的本质

    连接与通信,作为桥接中间件存在. 内部类和主体类可以无障碍通信: 1.通过继承连接实现: 2.通过接口连接通信: 形式: 1.命名空间: 2.运行上下文: 其它: 信息隐藏是次要功能. 内部类 Jav ...