六种获取配置properties文件的方法
总结一下六种获取配置properties文件的方法,代码如下:
package com.xujingyang.test ; import java.io.BufferedInputStream ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.util.Locale ;
import java.util.Properties ;
import java.util.PropertyResourceBundle ;
import java.util.ResourceBundle ;
import org.junit.Test ; /**
* @descript 六种获取配置文件的方法,注意各种的路径的问题
* @author xujingyang
* @time 2017年5月15日下午4:23:18
*/
public class TestReadProperties { /**
* 1、使用java.util.Properties类的load()方法
*/
@Test
public void read1(){
try {
InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties"));
Properties p=new Properties();
p.load(stream);
String name = p.get("name").toString() ;
String age = p.get("age").toString() ;
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 2、使用java.util.ResourceBundle类的getBundle()方法
*/
@Test
public void read2(){
try {
ResourceBundle bundle = ResourceBundle.getBundle("my", Locale.getDefault()) ;
String name = bundle.getString("name");
String age = bundle.getString("age");
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} /**
*3、 使用java.util.PropertyResourceBundle类的构造函数
*/
@Test
public void read3(){
try {
InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties"));
ResourceBundle bundle=new PropertyResourceBundle(stream);
String name = bundle.getString("name");
String age = bundle.getString("age");
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} /**
*4、 使用class变量的getResourceAsStream()方法
*/
@Test
public void read4(){
try {
InputStream stream = TestReadProperties.class.getResourceAsStream("/my.properties") ;
Properties p=new Properties();
p.load(stream);
String name = p.get("name").toString() ;
String age = p.get("age").toString() ;
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} /**
*5、 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
*/
@Test
public void read5(){
try {
InputStream stream = TestReadProperties.class.getClassLoader().getResourceAsStream("my.properties") ;
Properties p=new Properties();
p.load(stream);
String name = p.get("name").toString() ;
String age = p.get("age").toString() ;
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} /**
*6、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
*/
@Test
public void read6(){
try {
InputStream stream = ClassLoader.getSystemResourceAsStream("my.properties");
Properties p=new Properties();
p.load(stream);
String name = p.get("name").toString() ;
String age = p.get("age").toString() ;
System.out.println(name+":"+age) ;
} catch (Exception e) {
e.printStackTrace();
}
} }
配置文件:

name的值是小明,这里配置文件会自动编码
补充:
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
六种获取配置properties文件的方法的更多相关文章
- SpringMVC加载配置Properties文件的几种方式
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...
- springboot-项目获取resources下文件的方法
spring项目获取resources下文件的方法 最近写读取模板文件做一些后续的处理,将文件放在了项目的resources 下,发现了一个好用的读取方法: 比如上边是你需要读取的文件: 读 ...
- 解决IntelliJ IDEA无法读取配置*.properties文件的问题
idea对这些配置的文件方式很明显和eclipse是不同的.在idea中有一个 Content Roots的概念.需要为每一个folder配置相应的Content Roots.Content Root ...
- JS读取.properties文件的方法
假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1. 下载插件jquery.i18n.proper ...
- 在JavaScript文件中读取properties文件的方法
假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1. 下载插件jquery.i18n.proper ...
- java读取Properties文件的方法
resource.properties的内容: com.tsinkai.ettp.name=imooc com.tsinkai.ettp.website=www.imooc.com com.tsink ...
- 读取根目录src下的指定配置properties文件内容
代码如下: package com.chen.system.util; import java.io.File; import java.io.FileInputStream; import java ...
- 【java】获取解析资源文件的方法
关于资源文件的读取,有很多种方法,下面补充了多种方法 1.java.util.ResourceBundle 使用java自带的util包下的ResourceBundle类获取,使用方法最简单 //获取 ...
- Asp.NetCore3.1 WebApi 获取配置json文件中的数据
下面只是做一个简单的测试: 1:定义好appsetting.Json文件的配置信息如下: { "Logging": { "LogLevel": { " ...
随机推荐
- eclipse---个人设置
window---- preferences -----修改背景颜色 -----修改字体 ----修改窗口主题 ----设置编码 -----设置编译环境 ----设置web项目JDK编译的版本 --- ...
- ubuntu13.04中把ibus中的中文拼音输入设为默认
全新的ubuntu ,先选择 下载服务器 首选项->软件和更新 选择 最佳服务器 准备工作:卸载Ubuntu默认的ibus输入法: sudo apt-get remove ibus 然后添加Fc ...
- POJ - 3414 Pots 【BFS】
题目链接 http://poj.org/problem?id=3414 题意 给出两个杯子 容量分别为 A B 然后给出C 是目标容量 有三种操作 1 将一个杯子装满 2.将一个杯子全都倒掉 3.将一 ...
- iOS 基本数据类型 和 指针 特点
基本数据类型 : 整型int, 字符型char , 浮点型 (float 和 double), 枚举型; -- 构造类型 : 数组类型, 结构体类型, 共用体类型; -- 指针类型 : 最终要的数据类 ...
- 每天一个Linux命令(24)tar命令
tar命令可以为linux的文件和目录创建档案. (1)用法: 用法: tar [选项] [文件参数] (2)功能: 功能: 用来压缩和解压文件.tar本身不 ...
- Python 3 并发编程多进程之队列(推荐使用)
Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...
- 转战github了
现在已经改在github写博客了,地址为http://connorzhangxu.github.io/ 博客园用了几年,总体感觉不错,但是对公式的支持整体不是很好,所以后来自己搭建了github博客, ...
- MS-SQL charindex的用法
select * from table_a where charindex('a',id)>0 or charindex('b',id)>0 table_a 表中 id字段中含有" ...
- AngularJs 相应回车事件
最近做项目,要用到AngularJs,之前也有用过一点点,但仅限于数据的绑定,这次项目要整个前端需要使用这个框架,可能是不熟悉的原因,感觉这代码搞起来非常的不便利,:现总结一个响应回车事件: < ...
- R语言编程中的常见错误
R语言编程中的常见错误有一些错误是R的初学者和经验丰富的R程序员都可能常犯的.如果程序出错了,请检查以下几方面. 使用了错误的大小写.help().Help()和HELP()是三个不同的函数(只有第 ...