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());
}
}