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. 链接指示:extren"C"

    C++程序有时需要调用其他语言编写的函数,最常见的是调用C语言编写的函数.像所有其他名字一样,其他语言中的函数名字也必须在C++中进行声明,并且该声明必须指定返回类型和形参列表.对于其他语言编写的函数 ...

  2. SCAN----Redis检索键值对

    转载地址:http://redis.readthedocs.org/en/latest/key/scan.html SCAN SCAN cursor [MATCH pattern] [COUNT co ...

  3. ros使用pppoe拨号获取ipv6,并且下发IPV6的dns到客户机win7

    原文: https://www.mobile01.com/topicdetail.php?f=110&t=3405680 http://forum.mikrotik.com/viewtopic ...

  4. 20165230田坤烨网络对抗免考报告_基于WIN10的渗透攻击

    目录 简单信息收集 主机发现 ping nmap 端口扫描 nmap OS及服务版本探测 nmap -sV 绕过防火墙尝试 诱饵 随机数据长度 随机顺序扫描目标 MAC地址欺骗 实现win10的渗透攻 ...

  5. mysql 数据库中的每张表加同一个字段(避免重复加)

    DROP PROCEDURE IF EXISTS testEndHandle; DELIMITER $$ CREATE PROCEDURE testEndHandle() BEGIN DECLARE ...

  6. Prometheus安装部署说明

    本文主要介绍了如何二进制安装Prometheus.使用 Node Exporter 采集主机信息并使用Grafana来进行图形化的展示. 1. 安装Prometheus Server Promethe ...

  7. thinkPHP5如何使用rabbitmq

    thinkPHP5如何使用rabbitmq? 安装好 tp5 的 rabbitmq 扩展后,在项目根目录文件添加文件 rabbitmq.php 引导启动 rabbitmq. <?php defi ...

  8. 2、word插入目录、图/表

    一.word插入目录 依次对每个标题在“段落”中进行大纲级别选择. 光标定位于目录生成的页面,再“引用”->“目录”->选择“自动目录1/2”,则可自动生成目录.若目录有所更改,则可选择“ ...

  9. 词向量实践(gensim)

    词向量训练步骤: 分词并去停用词 词频过滤 训练 项目完整地址:https://github.com/cyandn/practice/tree/master/Word2Vec gensim中Word2 ...

  10. Java核心技术-读书笔记

    基本语法 Java中的所有函数都属于某个类的方法 Java没有任何无符号的int.long.short 或 byte 类型 浮点数值不适用于无法接受舍入误差的金融计算中,比如2.0-1.1不会输出想要 ...