目录

  一、前提条件

  二、使用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. 破解CentOS7的root及加密grub修复实战

    破解CentOS7的root及加密grub修复实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.破解CentOS7的root口令方案1 1>.启动时任意键暂停启动 2& ...

  2. Go入门:创建第一个Go工程

    前言 我是一名iOS开发. 因为公司后台都用的Go. 因为对服务端不了解. 所以想自己学习学习. 环境 因为自己的电脑是mac.然后在阿里云买的是centOS的服务器. 所以下面搭建的环境都是在cen ...

  3. 个人推荐的Java邮件配置

    # 大部分host都是smtp.加上你邮箱@后面的域名spring.mail.host=smtp.localhost.com spring.mail.username= spring.mail.pas ...

  4. centos设置开机自启动脚本

    1.新建脚本文件 我这里是为了设置开机自动设置ipv6隧道,所以命名为ipv6tunnel.sh ifconfig sit0 up ifconfig sit0 inet6 tunnel ::66.22 ...

  5. 推荐系统(recommender systems):预测电影评分--构造推荐系统的一种方法:低秩矩阵分解(low rank matrix factorization)

    如上图中的predicted ratings矩阵可以分解成X与ΘT的乘积,这个叫做低秩矩阵分解. 我们先学习出product的特征参数向量,在实际应用中这些学习出来的参数向量可能比较难以理解,也很难可 ...

  6. vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法

    vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法 重点:解决方法是: ...

  7. Linux中的CentOS 7克隆之后修改

    1.VMware Workstation软件查看克隆完成后的虚拟机网卡mac地址,记录下来 2.输入[cd /etc/sysconfig/network-scripts/]命令后,再执行[ip add ...

  8. 第一个python&selenium自动化测试实战项目

    说明:本项目采用流程控制思想,未引用unittest&pytest等单元测试框架 一.项目介绍 目的 测试某官方网站登录功能模块可以正常使用 用例 1.输入格式正确的用户名和正确的密码,验证是 ...

  9. Objective-C多态:动态类型识别+动态绑定+动态加载

    http://blog.csdn.net/tskyfree/article/details/7984887 一.Objective-C多态 1.概念:相同接口,不同的实现 来自不同类可以定义共享相同名 ...

  10. 命令行创建react.js项目

    npm install -g create-react-app  /*搭建一个全局的脚手架*/ create-react-app my-demo        /*创建项目 my-demo是项目名字* ...