java(包括springboot)读取resources下文件方式实现
作者:抄手砚 发布时间:2021-06-03 20:16:06
标签:java,读取,resources
本文主要介绍了java(包括springboot)读取resources下文件方式实现,分享给大家,具体如下:
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();
}
}
来源:https://www.cnblogs.com/whalesea/p/11677657.html


猜你喜欢
- 多说无益,贴代码:/** * 校验银行卡卡号 * * @param cardId &nbs
- 我已经很精简了,两篇(Spring Boot启动过程(一)、spring Boot启动过程(二))依然没写完,接着来。refreshCont
- /// <summary> /// 遍历Co
- ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口
- 网上的操作方式是我没有进行尝试,感兴趣的可以试试nohup java -Dserver.port=8848 -Dlogging.level.
- java代理有jdk * 、cglib代理,这里只说下jdk * ,jdk * 主要使用的是java反射机制(既java.lang.r
- 由于我经常下载一些pdf格式的电子书,有的时候一些好书下载下来没有书签,读起来感觉没有整体的感觉,所以决定自己写一个小工具,将特定格式的文本
- 前言sql注入是web开发中最常见的一种安全漏洞。可以用它来从数据库获取敏感信息、利用数据库的特性执行添加用户、导出文件等一系列恶意操作,甚
- 本文实例为大家分享了java绘制五子棋棋盘的具体代码,供大家参考,具体内容如下源码:import javax.imageio.ImageIO
- 本次数据请求使用postman, postman下载地址:https://www.getpostman.com/一、页面跳转1. 页面跳转@
- 本文实例为大家分享了Java实现斗地主的具体代码,供大家参考,具体内容如下import java.util.ArrayList;import
- 前言在框架中经常会会用到method.invoke()方法,用来执行某个的对象的目标方法。以前写代码用到反射时,总是获取先获取Method,
- 本文实例讲解了统计文本中某个字符串出现的次数或字符串中指定元素出现的次数方法,分享给大家供大家参考,具体内容如下运行效果图:程序查找的上此文
- 我们来简单实现一个cookie。一、简单介绍Cookie 是一些数据, 存储于你电脑上的文本文件中。当 web 服务器向浏览器发送 web
- 文档地址https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring
- 说明:此头像类似微信群组头像,整个头像由组内前N位人员的头像组合而成,可用网络或本地图片进行组合,最终显示为一个头像整体,看效果图:一、自定
- 对于ApplicationListener使用Spring的应该也熟悉,因为这就是我们平时学习的观察者模式的实际代表。Spring基于Jav
- 本文实例为大家分享了Android读写文件工具类的具体代码,供大家参考,具体内容如下public class Utils { p
- 1 C++类型转换本质1.1 自动类型转换(隐式)利用编译器内置的转换规则,或者用户自定义的转换构造函数以及类型转换函数(这些都可以认为是已
- NoHttp是专门做Android网络请求与下载的框架,NoHttp基本使用方法如下本文demo源码下载地址: http://xiazai.