java(包括springboot)读取resources下文件方式实现
作者:抄手砚 时间:2021-06-03 20:16:06
本文主要介绍了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
标签:java,读取,resources
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈MyBatis3 DynamicSql风格语法使用指南
2023-11-25 13:05:06
springboot 中整合mybatis多数据源不使用JPA
2023-03-01 08:43:02
![](https://img.aspxhome.com/file/2023/3/61823_0s.png)
如何使用Java redis实现发送手机验证码功能
2023-11-26 17:25:00
java 多线程死锁详解及简单实例
2022-02-25 19:48:52
Java实现LeetCode(54.螺旋矩阵)
2023-01-26 20:54:00
对int array进行排序的实例讲解
2021-12-09 06:51:15
![](https://img.aspxhome.com/file/2023/1/68101_0s.png)
Java String类字符串的理解与认知
2022-05-10 17:27:12
![](https://img.aspxhome.com/file/2023/5/60825_0s.png)
Java关于IO流的全面介绍
2023-08-12 08:14:46
![](https://img.aspxhome.com/file/2023/9/58069_0s.png)
Java SpringBoot高级用法详解
2021-07-03 18:27:09
![](https://img.aspxhome.com/file/2023/8/62998_0s.png)
Java线程的全方位详解
2023-04-11 14:02:55
![](https://img.aspxhome.com/file/2023/2/61042_0s.png)
详解JAVA流程控制语句
2023-11-05 02:27:09
![](https://img.aspxhome.com/file/2023/1/58711_0s.png)
如何将javaweb项目部署到linux下
2023-11-11 11:45:11
hadoop实现grep示例分享
2023-12-24 05:31:04
Mybatis Plus select 实现只查询部分字段
2022-07-23 18:51:32
![](https://img.aspxhome.com/file/2023/9/61279_0s.jpg)
OpenCV实现直线拟合
2023-06-22 15:22:37
![](https://img.aspxhome.com/file/2023/4/92494_0s.jpg)
Java判断字符串是否是整数或者浮点数的方法
2022-04-30 10:06:20
解决springboot启动失败的问题('hibernate.dialect' not set)
2023-11-09 03:21:21
java 配置MyEclipse Maven环境具体实现步骤
2021-07-31 04:12:23
详解Java内存泄露的示例代码
2023-06-08 03:34:51
Java(TM) Platform SE binary 打开jar文件的操作
2021-10-02 00:08:12
![](https://img.aspxhome.com/file/2023/4/62534_0s.jpg)