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 ...
随机推荐
- ESA2GJK1DH1K升级篇: 升级STM32 预热: 单片机每隔一定时间 使用 http 获取天气
前言: 实现功能概要: STM32使用AT指令控制Wi-Fi以TCP方式连接服务器(YY天气Web服务器),然后使用http的get协议获取今天的天气数据 单片机提取今天的温度和湿度数据,把温湿度数据 ...
- Flask视图之CBV示列
from flask import views, Flask app=Flask(__name__) class loginview( views.MethodView ): # 继承views. ...
- 4.28(TG模拟赛)总结
1.挖地雷 题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N≤20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任 ...
- connect ECONNREFUSED 127.0.0.1:80错误解决
这个报错也是一直困扰了我许久,服务端一直打印这个报错,但是页面数据响应又都正常,起初真不知道是因为什么原因,能看出来他是在调用80端口, 但是不明白为什么会调用80端口.一度以为是config.js里 ...
- pycharm默认注释与快捷键功能
pycharm快捷键使用技巧 Ctrl+d 复制当前行.或者选择的块Ctrl+n 跳转到类Ctrl+shift+n 快速查找文件名Ctrl+shift+f 全局查找,快速查找关键字的文件Ctrl+sh ...
- mysql(三)索引
参考文档:索引的基本操作 & 简单优化:https://www.cnblogs.com/zz-tt/p/6609828.html聚簇索引vs非聚簇索引:https://www.cnblogs. ...
- ng 手机验证码验证/发送(含倒计时)
ng 的手机号码进行验证: 1.在对应的ts文件中,先声明一个变量 private mobile: string private btnCaptchaText: string = '发送验证码' ...
- Controller如何进行重定向跳转
因为在Controller的返回都是默认走视图解析器的InternalResourceViewResolver,而视图解析器都是进行请求转发,需要在返回时地址前加入字符redirect: 视图解析器不 ...
- Scala 匹配模式
模式匹配 // Scala是没有Java中的switch case语法的,相对应的,Scala提供了更加强大的match case语法,即模式匹配,类替代switch case,match case也 ...
- [SOJ #686]抢救(2019-11-7考试)/[洛谷P3625][APIO2009]采油区域
题目大意 有一个\(n\times m\)的网格,\((x,y)\)权值为\(a_{x,y}\),要求从中选取三个不相交的\(k\times k\)的正方形使得它们权值最大.\(n,m,k\leqsl ...