1、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)

  File file = new File("src/main/resources/resource.properties");

  @Test
public void testReadFile2() throws IOException {
File file = new File("src/main/resources/resource.properties");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
} br.close();
isr.close();
fis.close();
}

2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)

File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);

    @Test
public void testReadFile3() throws IOException {
File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
} br.close();
isr.close();
fis.close();
}

3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)

Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();

    @Test
public void testReadFile() throws IOException {
// ClassPathResource classPathResource = new ClassPathResource("resource.properties");
Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
} br.close();
isr.close();
is.close();
}

4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)

package com.tsinkai.ettp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests { @Autowired
ResourceLoader resourceLoader; @Test
public void testReaderFile() throws IOException {
Resource resource = resourceLoader.getResource("classpath:resource.properties");
InputStream is = resource.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
} br.close();
isr.close();
is.close();
} }

java(包括springboot)读取resources下文件方式的更多相关文章

  1. Maven项目读取resources下文件的路径问题(getClassLoader的作用)

    读取resources下文件的方法 网上有问答如下:问: new FileInputStream("src/main/resources/all.properties") new ...

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

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

  3. SpringBoot读取Resource下文件的几种方式

    https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...

  4. springboot 读取resources下的文件然后下载

    记录下代码 /** * 下载模板 * * @param response * @param request */ @RequestMapping(value = "downloadTemp& ...

  5. java获取当前路径&读取当前目录下文件

    项目目录如下: test1.class中读取test.txt import java.io.*; import java.util.Scanner; public class Test1 { publ ...

  6. Maven项目读取resources下文件的路径

    要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = 类名.class.getClassLoader().getResource("conf ...

  7. Java项目读取resources资源文件路径那点事

    今天在Java程序中读取resources资源下的文件,由于对Java结构了解不透彻,遇到很多坑.正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径, ...

  8. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  9. springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)

    项目是spring-boot + spring-cloud 并使用maven 管理依赖.在springboot+maven项目下怎么读取resources下的文件实现文件下载? 怎么获取resourc ...

随机推荐

  1. ABP .net framework版 的发布

    先正常的发布流程走 特别的如下图

  2. Python实现电子词典

    代码一览: dictionary/├── code│   ├── client.py│   ├── func.py│   ├── server.py│   └── settings.py├── dat ...

  3. 第02组 Alpha冲刺(1/6)

    队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 如何进行团队代码的版本管理 如何使用微信云开发 如何使用管理微信开发团队 接下来的计划 沟通前后端成员,监督.提醒他们尽快 ...

  4. ValueError: Graph disconnected: cannot obtain value for tensor Tensor

    一般是Input和下面的变量重名了,导致model里面的input变成了第二次出现的Input变量,而不是最开始模型中作为输入的Input变量 改正方法:给第二个变量赋一个新名字即可

  5. 爬虫(一)基础知识(python)

    1.1 定义 网络爬虫,也叫网络蜘蛛(Web Spider),如果把互联网比喻成一个蜘蛛网,Spider就是一只在网上爬来爬去的蜘蛛.网络爬虫就是根据网页的地址来寻找网页的,也就是URL.举一个简单的 ...

  6. Python3.7 - Argparse模块的用法

    argparse 是一个命令行参数解析模块. argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数,当你的代码需要频繁地修改参数的时候,使用这个工具可以将参数和代码分离 ...

  7. 【转】android实时视频网络传输方案总结(一共有五套)

    最近研究了Android的实时视频网络传输问题,在视频处理方面花费了大量精力进行研究,总结出以下五套方案,并加以比较 以320×240大小的视频传输为例 方案 压缩率 压缩/传输方式 实时性 平均流量 ...

  8. golang 赋值与声明语法糖使用注意事项

    赋值与声明语法糖 基本用法略, 搜索即可 注意事项 类型推断 := 会自动进行类型推断, 当想要的类型不是自己想要的类型时需要进行类型转换 // i1 默认是 int 类型 i1 := 1 // 当需 ...

  9. ng 手机验证码验证/发送(含倒计时)

    ng 的手机号码进行验证: 1.在对应的ts文件中,先声明一个变量 private mobile: string private btnCaptchaText: string = '发送验证码'   ...

  10. 在vue-cli中安装element

    在vue-cli中安装elemnent-ui 的步骤 在安装vue-cli 的基础上进行安装 1.npm i element-ui -S 安装element-cli 安装之后会在项目中的node_mo ...