配置文件 
放置在src下面 obj.properties

className=com.store.order.dao.impl.OrderDaoImpl


方式一

@Test
public void test1() throws Exception{
//文件放在src下面.eclipse会自动拷贝一份到bin目录下,或者build/classes下面,
InputStream is = Class.forName("com.store.test.test").getClassLoader().getResourceAsStream("obj.properties");
Properties properties = new Properties();
properties.load(is);
String className = properties.getProperty("className");
System.out.println(className);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方式二

    /**
* 不推荐使用,文件位置被固定了,只能在本机使用,我们把class文件拷贝给别人就不能使用了
* @throws Exception
*/
@Test
public void test2() throws Exception{
FileInputStream fis = new FileInputStream("D:\\workspaces\\store_v1.0\\src\\obj.properties");
Properties properties = new Properties();
properties.load(fis);
String className = properties.getProperty("className");
System.out.println(className);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

方式三

    @Test
public void test3() throws Exception{
//读取配置文件,只能读取默认后缀名为properties(不写)
//如果有包,写法是com.store.obj
//细节:他只能读取类路路径下的文件,不能写入
ResourceBundle bundle = ResourceBundle.getBundle("obj");
String className = bundle.getString("className"); //通过键获取值
System.out.println(className);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方式四

/**
* Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源
* 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启
*/
private static void springUtil(){
Properties props = new Properties();
while(true){
try {
props=PropertiesLoaderUtils.loadAllProperties("message.properties");
for(Object key:props.keySet()){
System.out.print(key+":");
System.out.println(props.get(key));
}
} catch (IOException e) {
System.out.println(e.getMessage());
} try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

java读取配置文件常用的四种方式的更多相关文章

  1. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  2. JAVA中集合输出的四种方式

    在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...

  3. 【开发笔记】- Java读取properties文件的五种方式

    原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...

  4. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  5. Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...

  6. 【转】Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...

  7. 160624、Spark读取数据库(Mysql)的四种方式讲解

    目前Spark支持四种方式从数据库中读取数据,这里以Mysql为例进行介绍. 一.不指定查询条件 这个方式链接MySql的函数原型是: 1 def jdbc(url: String, table: S ...

  8. Java Enum枚举 遍历判断 四种方式(包括 Lambda 表达式过滤)

    示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...

  9. Java中遍历Map的四种方式

    Demo如下 Map<String, String> map = new HashMap<>(); map.put("key1","data1&q ...

随机推荐

  1. Ubuntu16.04 问题汇总

    Ubuntu16.04安装wps并解决系统缺失字体问题 http://www.cnblogs.com/liutongqing/p/6388160.html

  2. 参加2018之江杯全球人工智能大赛
:视频识别&问答

    学习了一段时间的AI,用天池大赛来检验一下自己的学习成果. 题目:参赛者需对给定的短视频进行内容识别和分析,并回答每一个视频对应的问题.细节请到阿里天池搜索. 两种思路 1 将视频截成一帧一帧的图片, ...

  3. leetcode_day01

    任务一:有效的括号 题目链接:https://leetcode-cn.com/problems/valid-parentheses/ 自己的答案: class Solution: def isVali ...

  4. Linux查看端口被占用情形

    查看某端口的占用情况: lsof -i:<端口号> 例如:lsof -i:8080 netstat -apn|grep <端口号> 例如: netstat -apn | gre ...

  5. js中prop和attr区别

    首先 attr 是从页面搜索获得元素值,所以页面必须明确定义元素才能获取值,相对来说比较慢. 如: <input name='test' type='checkbox'> $('input ...

  6. Snakes and Ladders LightOJ - 1151( 概率dp+高斯消元)

    Snakes and Ladders LightOJ - 1151 题意: 有100个格子,从1开始走,每次抛骰子走1~6,若抛出的点数导致走出了100以外,则重新抛一次.有n个格子会单向传送到其他格 ...

  7. EAR、JAR、WAR(IT)

    EAR文件包括整个项目,内含多个ejb module(jar文件)和web module(war文件)   JAR.WAR.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压 ...

  8. android工程下assets与raw文件夹

    在应用的开发中,当我们创建一个新的Android工程后,我们会发现工程中包含了一个目录assets,另外当我们察看一些示例工程的时候,有时会发现在该工程的资源目录下会有一个raw目录(res/raw) ...

  9. Python学习笔记(Django篇)——2、创建第一个django应用

    在helloworld工程的基础上,我们准备创建第一个基于web的django应用,把这个应用名字命名为demo   首先,是如何创建这个应用包,当然第一选择我们可以进入到cmd中,执行如下命令 py ...

  10. Location of ESXi 5.1 log files

    Purpose This article provides the default location of log files on an ESXi 5.1 host. For other produ ...