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文件的方法的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  3. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  4. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  5. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  6. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

  9. Java读取Properties文件的六种方法

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...

随机推荐

  1. 【贪心】【P5521】[yLOI2019] 梅深不见冬

    B [yLOI2019] 梅深不见冬 Background 风,吹起梅岭的深冬:霜,如惊涛一样汹涌:雪,飘落后把所有烧成空, 像这场,捕捉不到的梦. 醒来时已是多年之久,宫门铜环才长了铁锈, 也开始生 ...

  2. es 内存占用优化

    对6.3: 修改Elasticsearch中JVM配置文件jvm.options: Dlog4j2.enable.threadlocals=false 注: 本文主要针对ES 2.x. “该给ES分配 ...

  3. 【07月02日】A股滚动市盈率PE最低排名

    ​仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 方大集团(SZ000055) - 滚动市盈率PE:2.68 - 滚动市净率PB:1.2 - 滚动年化股息收益率:3.78% - 建筑产 ...

  4. AVLMap平衡二叉树

    public class AVLMap<K, V> implements Iterable<AVLEntry<K, V>> { private int size; ...

  5. javascript 函数的暂停和恢复

    javascript 异步编程从来都是一个难题,最开始我们用 callback,但随之触发了回调地狱,于是"发明" Promise 解决 callback 嵌套过深的问题.然而由于 ...

  6. excel绘制多列 其中一列作为横坐标 ; 数值拟合

    excel绘制多列,其中最左列作为横纵坐标: 选中很多列,然后,,点击菜单栏的“插入”->“图标” -->在弹出的“插入图表”对话框中选择“X Y(散点图)”,默认是条形图.  左边的列会 ...

  7. EXT.net 1.x TreePanel的一个坑

    Ext.net TreePanel有一个方法 drptreepanel.setChecked({ ids: idsarray, silent: true });  如果TreePanel里有1,3两个 ...

  8. Java学习:构造方法

    构造方法: 构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是再调用构造函数. 格式: public 类名称(参数类型 参数名称){ 方法体 } 注意事项: 构造方法的名称 ...

  9. Replication:事务复制 Subscriber的主键列是只读的

    在使用Transactional Replication时,Subscriber 被认为是“Read-Only”的 , All data at the Subscriber is “read-only ...

  10. 关于.net core 中的signalR组件的使用

    SignalR是为了提供更方便的web交互响应式到推送式的解决方案.有了它之后可以实现客户端直接调用服务端的方法并且获得返回值 (客户端可以是各种平台,目前SignalR支持的语言版本有C#.java ...