Spring项目读取resource下的文件
目录
一、前提条件
2.1、Controller、service中使用ClassPathResource
一、前提条件
要去读取的文件是存放在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下的文件的更多相关文章
- SpringBoot项目构建成jar运行后,如何正确读取resource下的文件
SpringBoot项目构建成jar运行后,如何正确读取resource下的文件 不管你使用的是SpringBoot 1.x还是SpringBoot2.x,在开Dev环境中使用eclipse.IEAD ...
- springboot项目获取resource下的文件
package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...
- Spring Boot 读取 resource 下文件
支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...
- Springboot项目读取resource下的静态资源方法
如果按相对路径直接读会定位到target下,因为springboot打包后读到这里 如果做单元测试的话是找不到文件的 File jsonFile = ResourceUtils.getFile(&qu ...
- Maven 工程读取resource下的文件
1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...
- springboot读取resource下的文件
public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...
- springboot 读取 resource 下的文件
ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...
- SpringBoot读取Resource下文件的几种方式(十五)
需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...
- maven工程读取resource下配置文件
maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources 下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...
随机推荐
- golang测试
简述 Go语言中自带有一个轻量级的测试框架testing和自带的go test命令来实现单元测试和性能测试. go test [-c] [-i] [build flags] [packages] [f ...
- CentOS7 编译安装MySQL5.6.38(一)
一.下载MySQL5.6.38安装包 下载地址:https://www.mysql.com/downloads/ 打开网站之后选择Archives 然后再选择开源版本 选择我们要下载的版本: htt ...
- 石子合并问题--直线版 HRBUST - 1818
t题目链接:https://vjudge.net/problem/HRBUST-1818 思路:一段已经合并的区间,分成两段区间,遍历所有能分开的区间. 代码有注释,基本就这样一个简单是思路. #in ...
- httprunner学习19-重复执行用例
前言 使用httprunner做接口测试过程中,在工作中会遇到这种场景,发现某个接口不稳定,想重复运行100次用例,甚至1000次,看成功率. yml脚本 - config: name: httpbi ...
- css绘制各种图形,三角形,长方形,梯形
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8cAAADVCAIAAAD1mxUAAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWX ...
- 地址栏参数获取函数 GetQueryStr(name)
//name:参数名称,return:有则返回该参数对应值,没有则返回null function GetQueryStr(name) { var reg = new RegExp(& ...
- UI系统的表示与维护
UI系统的表示与维护: 渲染单元的组织.维护.交互.解释.渲染. UI系统在应用层连接着视图的表示,在系统层连接着视图的绘制. 一.UI的结构 树形结构 二.UI的描述: 1.UI系统或UIkit或U ...
- Dockerfile介绍、Docker制作jdk镜像
Dockerfile介绍.Docker制作jdk镜像 目标 1.Dockerfile简介 2.Docker制作jdk镜像 Dockerfile简介 dockerfile 是一个文本格式的配置文件, 用 ...
- Edraw Max 9.4 Crack Method
使用010editor修改以下两个文件. BaseCore.dll (修改二进制内容hex) Before C6 45 C8 62 C6 45 C9 64 C6 45 CA 65 C6 45 CB 6 ...
- cloudevents 通用event 描述指南
cloudevents 是由cncf 组织管理的一个通用event描述指南 特性: 一致性 可理解性 可移植性 说明 cloudevents 不仅提供了核心描述,同时也包含了不同协议的指南说明(htt ...