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. Python 的AES加密与解密

    AES加密方式有五种:ECB, CBC, CTR, CFB, OFB 从安全性角度推荐CBC加密方法,本文介绍了CBC,ECB两种加密方法的python实现 python 在 Windows下使用AE ...

  2. ##C++ format 格式化字符串

    C++ format 格式化字符串实现方式 1. http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprint ...

  3. js 校验手机号码格式

      手机号码格式简单校验 原理:判断手机号是否以已经发行的手机号码段开头,而且判断其余9位是否是数字. 方式一: var phone = $('#phone').val(); var regex = ...

  4. makefile小计

    1. makefile格式如下,想要生成code.o,就要指定它依赖哪些东西.比如这里是依赖code.cpp这个文件 然后如何生成,换行+tab(与python类似,通过tab区分下面的是生成的命令) ...

  5. git bash 乱码问题之解决方案

    解决办法:右击左上方git标识,然后进入到如图中,点击Text,进行操作. 操作完毕后,关闭git bash,然后再重新打开,执行ls或ll命令,查看对应的以中文作为目录或文件名是否显示乱码,如果之前 ...

  6. Ladon内网渗透扫描器PowerShell版

    程序简介 Ladon一款用于大型网络渗透的多线程插件化综合扫描神器,含端口扫描.服务识别.网络资产.密码爆破.高危漏洞检测以及一键GetShell,支持批量A段/B段/C段以及跨网段扫描,支持URL. ...

  7. BBC micro:bit引脚介绍

    另外两个大引脚(3V和GND)是非常不同的! 注意 标记为3V和GND的引脚与电路板的电源相关,千万不要连接在一起. 电源输入:如果BBC micro:bit由USB或电池供电,则可以使用3V引脚作为 ...

  8. pycharm社区办找不到View-->Tool Windows->DataBase的解决办法

    File->Settings->Plugins->Database Navigator 安装

  9. Visual Studio 重命名项目名

    1. 打开VS Studio,重命名项目 2. 重命名对应的项目文件夹,并重命名项目文件夹下的这两个文件名: 3. 用记事本打开解决方案,修改对应的项目名字和路径 未完 ...... 点击访问原文(进 ...

  10. C++ 数组输出

    C++中输出数组数据分两种情况:字符型数组和非字符型数组 当定义变量为字符型数组时,采用cout<<数组名; 系统会将数组当作字符串来输出,如: ]={'}; cout << ...