总结一下六种获取配置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文件的方法的更多相关文章

  1. SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...

  2. springboot-项目获取resources下文件的方法

    spring项目获取resources下文件的方法   最近写读取模板文件做一些后续的处理,将文件放在了项目的resources 下,发现了一个好用的读取方法:   比如上边是你需要读取的文件:  读 ...

  3. 解决IntelliJ IDEA无法读取配置*.properties文件的问题

    idea对这些配置的文件方式很明显和eclipse是不同的.在idea中有一个 Content Roots的概念.需要为每一个folder配置相应的Content Roots.Content Root ...

  4. JS读取.properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

  5. 在JavaScript文件中读取properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

  6. java读取Properties文件的方法

    resource.properties的内容: com.tsinkai.ettp.name=imooc com.tsinkai.ettp.website=www.imooc.com com.tsink ...

  7. 读取根目录src下的指定配置properties文件内容

    代码如下: package com.chen.system.util; import java.io.File; import java.io.FileInputStream; import java ...

  8. 【java】获取解析资源文件的方法

    关于资源文件的读取,有很多种方法,下面补充了多种方法 1.java.util.ResourceBundle 使用java自带的util包下的ResourceBundle类获取,使用方法最简单 //获取 ...

  9. Asp.NetCore3.1 WebApi 获取配置json文件中的数据

    下面只是做一个简单的测试: 1:定义好appsetting.Json文件的配置信息如下: { "Logging": { "LogLevel": { " ...

随机推荐

  1. LeetCode:有效三角形的个数【611】

    LeetCode:有效三角形的个数[611] 题目描述 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有 ...

  2. deviceToken的获取(二)

    第一步:申请证书: 第二步:申请app ids,应用名字必须一致.然后再进入进行编辑,使其enable,绿灯. 第三步:申请provisioning profile,生成.mobileprovisio ...

  3. ubuntu14.04搭建gitlab

    以下内容来自:https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/  (清华大学开源软件镜像站)可以直接移步上面的网站.这里做个笔记,也是为了记录一下 ...

  4. dojo 官方翻译 dojo/json 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/json.html#dojo-json require(["dojo/json&q ...

  5. 算法(Algorithms)第4版 练习 1.5.10

    Yes, but it could increase the tree height, so the performance guarantee would be invalid.

  6. Linux- 自动备份MySQL数据库脚本

    数据安全很重要,所以日常中需要对数据库进行备份.

  7. HTML5 学习记录——2

    20150826 1.声明文档类型 <!DOCTYPE>  声明HTML是用什么版本写的. 常用声明; 2.HYML头部元素   <head> <title> 定义 ...

  8. 201621123014《JAVA程序设计》第2周学习总结

    1. 本周学习总结 引用数据类型:JAVA定义字符串实际上是创建字符串的引用,将引用指向需要的字符串. 字符串常量池:直接对引用赋值时,会先在字符串中搜索是否有这个对象,已有则不创建直接指向它. St ...

  9. BEC listen and translation exercise 43

    Reach for the stars so if you fall you land on a cloud.飞向星空吧,就算坠落,接住你的也是云彩. Anyway, exam failure can ...

  10. linux命令学习笔记(39):grep 命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来. grep全称是Global Regular Expression Print,表示全局正则表 ...