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 ...
随机推荐
- 8-12 canvas专题-阶段练习一(上)
8-12 canvas专题-阶段练习一(上) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...
- go语言---defer
go语言---defer https://blog.csdn.net/cyk2396/article/details/78885135 defer 是在函数退出前调用,多个defer遵循 先进后出 的 ...
- 【POJ 1330】 Nearest Common Ancestors
[题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...
- linux下的C语言开发 GDB的例子
在很多人的眼里,C语言和linux常常是分不开的.这其中的原因很多,其中最重要的一部分我认为是linux本身就是C语言的杰出作品.当然,linux操作系统本身对C语言的支持也是相当到位的.作为一个真正 ...
- 框架-Java:Spring Boot
ylbtech-框架-Java:Spring Boot 1.返回顶部 1. Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该 ...
- IntelliJ IDEA Tomcat配置
解决方法: 下载地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/native
- JAVA的引用类型变量(C/C++中叫指针)
任何变量只要存在内存中,就需要有一个唯一的编号标识这个变量在内存中的位置,而这个唯一的内存编号就是内存地址,内存地址就是所谓的指针!
- bzoj 1596: [Usaco2008 Jan]电话网络【贪心】
dfs,如果一个点的儿子.本身.父亲都没有塔,就在父亲上建一个 原理不明-- #include<iostream> #include<cstdio> using namespa ...
- lodop打印图片
LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); //LODOP. ...
- 公司5:JrVue表格
组件名称:jr-dynamic-query-table 组件布局 table组件名称: jr-dynamic-query-table 分页组件名称: el-pagination <div re ...