java读取配置文件常用的四种方式
配置文件
放置在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读取配置文件常用的四种方式的更多相关文章
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- JAVA中集合输出的四种方式
在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
- Java实现文件复制的四种方式
背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...
- Java遍历Map对象的四种方式
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...
- 【转】Java遍历Map对象的四种方式
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...
- 160624、Spark读取数据库(Mysql)的四种方式讲解
目前Spark支持四种方式从数据库中读取数据,这里以Mysql为例进行介绍. 一.不指定查询条件 这个方式链接MySql的函数原型是: 1 def jdbc(url: String, table: S ...
- Java Enum枚举 遍历判断 四种方式(包括 Lambda 表达式过滤)
示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...
- Java中遍历Map的四种方式
Demo如下 Map<String, String> map = new HashMap<>(); map.put("key1","data1&q ...
随机推荐
- 从今天开始学Python
外部链接下载吧 1. Python 3.63.chm AIP 帮助文档 下载:https://pan.baidu.com/s/1lhpv8JTC3Z7B6aZ3qQi40g 2. VMwar ...
- 【视觉SLAM14讲】ch4心得与课后题答案【仅供参考】
答案: Q1:验证SO(3) SE(3) Sim(3)关于乘法成群 SO(3) : 由P64最开始可知,乘法代表了旋转,而SO(3)是旋转矩阵的集合, SE(3) Sim(3) 同理(最基础的部分 ...
- ES原理(转载)
该博客属于转载,是很经典的一篇关于ES的介绍: Elasticsearch 是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分 ...
- PHP高级——抽象类与接口的区别
在学习PHP面向对象时,都会在抽象类与接口上迷惑,作用差不多为什么还那么容易混淆,何不留一去一?但是事实上两者的区别还是很大的,如果能够很好地运用PHP的两个方法,面向对象的程序设计将会更加合理.清晰 ...
- BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】
题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...
- 虚拟机——mnt_hgfs下无目录情况解决
/mnt/hgfs下无目录情况解决: VMware8虚拟机安装Ubuntu 11.10使用share folders共享目录将虚拟机掉电关闭(不能暂停),设置share folders目录,重启虚拟机 ...
- let与const区别
let 1. let有变量提升,但是有约束 2. 会形成暂时性死区(TDZ) 3. 同一个块级作用域内不允许声明相同变量 4. 块级变量 5. let声明的全局变量不是全局对象的属性,var会 6. ...
- Problem b
Problem b 题目描述 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 输入 第一行一个整数 ...
- Codeforces 934.D A Determined Cleanup
D. A Determined Cleanup time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Waifu2x测试
真的是好玩..给大家(?)提供一个能用Waifu2x upscale的图片类型集合.. 上面是原图下面是Upscaled..因为是png大家自行下载对比..