springboot读取src下文件_java(包括springboot)读取resources下文件方式

本文介绍了SpringBoot中读取resources下文件的五种方法:1) 使用项目内路径,仅限开发环境;2) 利用ResourceUtils,在Linux环境下可能失效;3) 通过ClassPathResource,适用于各种环境;4) 结合Spring注解的ResourceLoader;5) 创建资源类结合SpringBoot注解进行读取。
摘要由CSDN通过智能技术生成

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

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

@Testpublic void testReadFile2() throwsIOException {

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

FileInputStream fis= newFileInputStream(file);

InputStreamReader isr= newInputStreamReader(fis);

BufferedReader br= newBufferedReader(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);

@Testpublic void testReadFile3() throwsIOException {

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

FileInputStream fis= newFileInputStream(file);

InputStreamReader isr= newInputStreamReader(fis);

BufferedReader br= newBufferedReader(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();

@Testpublic void testReadFile() throwsIOException {//ClassPathResource classPathResource = new ClassPathResource("resource.properties");

Resource resource = new ClassPathResource("resource.properties");

InputStream is=resource.getInputStream();

InputStreamReader isr= newInputStreamReader(is);

BufferedReader br= newBufferedReader(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;类的注解。

packagecom.tsinkai.ettp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.core.io.Resource;importorg.springframework.core.io.ResourceLoader;importorg.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTestpublic classEttpCustomApplicationTests {

@Autowired

ResourceLoader resourceLoader;

@Testpublic void testReaderFile() throwsIOException {

Resource resource= resourceLoader.getResource("classpath:resource.properties");

InputStream is=resource.getInputStream();

InputStreamReader isr= newInputStreamReader(is);

BufferedReader br= newBufferedReader(isr);

String data= null;while((data = br.readLine()) != null) {

System.out.println(data);

}

br.close();

isr.close();

is.close();

}

}

5、结合springboot注解,自定义一个资源类,可以使用get/set方式读取单个属性。

需要根据具体资源文件内容创建资源类,和对应属性

resource.properties内容

com.tsinkai.ettp.name=imooc

com.tsinkai.ettp.website=www.imooc.com

com.tsinkai.ettp.language=java

资源类:

packagecom.imooc.pojo;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;

@Configuration

@ConfigurationProperties(prefix="com.imooc.opensource")//属性前缀

@PropertySource(value="classpath:resource.properties")//文件路径

public classResource {privateString name;privateString website;privateString language;publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getWebsite() {returnwebsite;

}public voidsetWebsite(String website) {this.website =website;

}publicString getLanguage() {returnlanguage;

}public voidsetLanguage(String language) {this.language =language;

}

}

调用方式:

packagecom.tsinkai.ettp;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.BeanUtils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importcom.tsinkai.ettp.common.Resource;

@RunWith(SpringRunner.class)

@SpringBootTestpublic classEttpCustomApplicationTests {

@Autowired

Resource customResource;

@Testpublic voidreadResource() {

Resource bean= newResource();

BeanUtils.copyProperties(customResource, bean);

System.out.println(bean.getName());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值