java加载properties文件的六种方法总结
java加载properties文件的六种方法总结
java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载;
另一种是通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载。
注意:一定要区分路径格式
实现代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.util;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;public class PropertiesUtil { private static String basePath = "src/prop.properties"; private static String name = ""; private static String nickname = ""; private static String password = ""; /** * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件 * */ public static String getName1() { try { Properties prop = new Properties(); InputStream is = new FileInputStream(basePath); prop.load(is); name = prop.getProperty("username"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 二、 使用class变量的getResourceAsStream()方法 * 注意:getResourceAsStream()读取路径是与本类的同一包下 * */ public static String getName2() { Properties prop = new Properties(); InputStream is = PropertiesUtil.class .getResourceAsStream("/com/util/prop.properties"); try { prop.load(is); name = prop.getProperty("username"); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 三、 * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀 否则会报空指针异常 * */ public static String getName3() { Properties prop = new Properties(); InputStream is = PropertiesUtil.class.getClassLoader() .getResourceAsStream("com/util/prop.properties"); try { prop.load(is); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 四、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 * getSystemResourceAsStream()方法的参数格式也是有固定要求的 * */ public static String getName4() { Properties prop = new Properties(); InputStream is = ClassLoader .getSystemResourceAsStream("com/util/prop.properties"); try { prop.load(is); name = prop.getProperty("username"); } catch (IOException e) { e.printStackTrace(); } return name; } /** * 五、 使用java.util.ResourceBundle类的getBundle()方法 * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常 * */ public static String getName5() { ResourceBundle rb = ResourceBundle.getBundle("com/util/prop"); password = rb.getString("password"); return password; } /** * 六、 使用java.util.PropertyResourceBundle类的构造函数 * */ public static String getName6() { try { InputStream is = new FileInputStream(basePath); ResourceBundle rb = new PropertyResourceBundle(is); nickname = rb.getString("nickname"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return nickname; } /** * 测试 * */ public static void main(String[] args) { System.out.println("name1:" + PropertiesUtil.getName1()); System.out.println("name2:" + PropertiesUtil.getName2()); System.out.println("name3:" + PropertiesUtil.getName3()); System.out.println("name4:" + PropertiesUtil.getName4()); System.out.println("password:" + PropertiesUtil.getName5()); System.out.println("nickname:" + PropertiesUtil.getName6()); }} |
java加载properties文件的六种方法总结的更多相关文章
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- Java加载资源文件几种方法
from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...
- Java加载资源文件的两种方法
处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...
- 加载properties文件的三种方法
源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...
- JAVA加载Properties配置资源文件
JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...
- xml文件 加载properties文件的两种方法与注意事项
1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException 和 java.lang. ...
- Java读取Properties文件的六种方法
使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...
- Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件
前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...
- spring入门(二)【加载properties文件】
在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...
随机推荐
- kentico7中设置site的default page
在SiteManager中,Settings中,选择好站点,然后Content,Default alias path
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- ubuntu将mysql、nginx添加到环境变量中
vim /etc/profile 添加 export PATH="$PATH:/usr/local/mysql/bin" export PATH="$PATH:/usr/ ...
- Java 高级数据结构 —— Properties
1. Properties Properties 是 Java 的内置实现: public class Properties extends Hashtable<Object,Object> ...
- ZOJ1450 Minimal Circle 最小圆覆盖
ZOJ1450 给定N个点(N<=100)求最小的圆把这些点全部覆盖 考虑对于三角形,可以唯一的找到外接圆,而多边形又可以分解为三角形,所以对于多边形也可以找到唯一的最小覆盖圆. #includ ...
- 2017年最新VOS2009/VOS3000最新手机号段导入文件(手机归属地数据)
VOS2009.vos3000.vos5000最新手机号段归属地数据库导入文件. 基于2017年4月最新版手机号段归属地制作 共360569条记录,兼容所有版本的昆石VOS,包括VOS2009.vos ...
- bzoj 1592: [Usaco2008 Feb]Making the Grade 路面修整【dp】
因为是单调不降或单调不升,所以所有的bi如果都是ai中出现过的一定不会变差 以递增为例,设f[i][j]为第j段选第i大的高度,预处理出s[i][j]表示选第i大的时,前j个 a与第i大的值的差的绝对 ...
- array_column() 函数[二维数组转为一维数组]
array_column() 函数 输出数组中某个键值的集合[二维数组转为一位数组] <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' =&g ...
- daily_journal_2 神奇的一天
写博客日记的第二天,第一天立的flag开始有点松动啦,继续坚持啊!坚持就是胜利. 今天真是神奇的一天,上午的计划是照常进行的,但是前天淋雨赶上风寒,又吃了新疆室友的大补特产,龙体开始感觉到不适,于是上 ...
- 【JAVA 学习笔记2】if使用例子
int a =3; if (a%2==0) { System.out.println(a+" 是偶数"); System.out.println(a+" 不是奇数&quo ...