java读取Properties文件的方法
2024-09-02 07:44:32
resource.properties的内容:
com.tsinkai.ettp.name=imooc
com.tsinkai.ettp.website=www.imooc.com
com.tsinkai.ettp.language=java
1、使用java.util.Properties的load(InputStream inStream)方法。
先读取文件生成inputStream流,再用load加载。
@Test
public void testReadProperties3() throws IOException {
Properties properties = new Properties();
// InputStream inputStream = this.getClass().getResourceAsStream("/resource.properties");
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("resource.properties");
Resource resource = new ClassPathResource("resource.properties");
InputStream inputStream = resource.getInputStream(); properties.load(inputStream);
System.out.println(properties.getProperty("com.tsinkai.ettp.name"));
}
2、使用org.springframework.core.io.support.PropertiesLoaderUtils;
@Test
public void testReadProperties() throws IOException {
Properties properties = PropertiesLoaderUtils.loadAllProperties("resource.properties");
String name = properties.getProperty("com.tsinkai.ettp.name");
System.out.println(name);
}
3、使用java.util.ResourceBundle;
@Test
public void testReadProperties2() throws IOException {
ResourceBundle resourceBundle = ResourceBundle.getBundle("resource");
String name = resourceBundle.getString("com.tsinkai.ettp.name");
System.out.println(name);
}
4、使用spring注解创建资源类
资源类:
package com.imooc.pojo; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; @Configuration
@ConfigurationProperties(prefix="com.imooc.opensource")//属性前缀
@PropertySource(value="classpath:resource.properties")//文件路径
public class Resource {
private String name;
private String website;
private String language; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
调用:
package com.tsinkai.ettp; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.tsinkai.ettp.common.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests { @Autowired
Resource customResource; @Test
public void readResource() {
Resource bean = new Resource();
BeanUtils.copyProperties(customResource, bean);
System.out.println(bean.getName());
}
}
最新文章
- SpringMVC 文件上传
- svn记录删除
- 分组函数 ----group by 使用总结
- 页面加载后resize页面布局
- 基础总结篇之三:Activity的task相关
- jquery异步上传文件,支持IE8
- 常见css的兼容问题
- 【pyhton】import math与import cmath
- Udacity调试课笔记之断言异常
- 达到J2EE在后台action控制接待javascript弹出的对话框
- SVN命令汇总
- Samba匿名用戶仅仅唯读访问
- wx小程序初体验
- Select的逻辑处理顺序(Transact-SQL)
- 小命令tac、cat、rev的用法
- 关于springmvc配置validator的注意事项
- python变量命名规则
- JavaScript实现接口的三种经典方式
- SpringBoot数据库集成-Mybatis
- APP需求调研、对比
热门文章
- I/O管理杂记
- Splay的基本操作(插入/删除,查询)
- qbxt济南七日(游)学习
- Kafka Rebalance机制分析
- requests.session()会话保持
- react + node + express + ant + mongodb 的简洁兼时尚的博客网站
- 【Activiti学习之五】BPMN事件
- 批处理中setlocal enabledelayedexpansion的作用详细整理
- 《Linux就该这么学》培训笔记_ch09_使用ssh服务管理远程主机
- QuantLib 金融计算——案例之普通欧式期权分析