java读取Properties文件的方法

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());
}
}
java读取Properties文件的方法的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- Java读取Properties文件的六种方法
使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...
随机推荐
- 2-OpenResty 安装使用(Windows)
下载 OpenResty 1. https://gitee.com/yang456/LearnOpenResty.git 2. http://openresty.org/cn/download.h ...
- 请用正则实现String.trim()
String.prototype.trim1=function(){ return this.replace(/(^\s*)|(\s*$)/g,""); }; 写一个functio ...
- 基环树DP
基环树DP Page1:问题 啥是基环树?就是在一棵树上增加一条边. Page2:基环树的几种情况 无向 有向:基环外向树,基环内向树. Page3:处理问题的基本方式 1.断环成树 2.分别处理树和 ...
- PCM存储格式 Intel 和 Motorola
https://blog.csdn.net/cxz_yzxkj/article/details/84496614
- 【CF848B】 Rooter's Song
题目链接 \(solution\) 类似于蚂蚁那道题的做法 弹性碰撞相当于交换位置并继续前进,考虑一个起点\((x,0)\),时间为\(t\)出发的\(dancer\),相当于从\((x,-t)\)的 ...
- C/C++中书写汇编指令
汇编语言的指令格式目前有两种不同的标准:Windows下的汇编语言基本上都遵循Intel风格的语法,比如:MASM.NASM,Unix/Linux下的汇编语言基本上都遵循AT&T风格的语法. ...
- 关联分析-MIC
MIC:the Maximal Information Coefficient,是用网格分法判断数据的集中程度的一个标准. MIC所依据的理念是,如果2个变量之间存在着一种关系,那么就应该有一种方法在 ...
- 将物理机转换成vmware虚机
随着虚拟化的快速发展,公司主要是以公有云+私有云结合的混合云部署,据我不成熟的了解,目前很少有公司会将一台单独的物理机作为服务器,在公司内部大家逐渐接受了私有云的部署方案,这样做不但可以节省硬件资源, ...
- mongodb compass 启动报错()
报错: 原因:由于直接关闭客户端,进程没关导致下次开启时,后台还是运行的所以无法重新开启 解决办法: 1.查看进程 tasklist | findstr “MongoDBCompass.exe” 2. ...
- ndk-build官方使用说明
ndk-build 脚本可用于编译采用 NDK 基于 Make 的编译系统的项目.此外,我们还针对 ndk-build 使用的 Android.mk和 Application.mk 配置提供了更具体的 ...