How to load Properties file from a static block or static method
来源:互联网 发布:淘宝客服售前售后要点 编辑:程序博客网 时间:2024/06/05 14:25
通常,通用类会从静态上下文读取属性文件,这下面是一个例子
mport
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Properties;
public
class
MyPropInStaticBlock {
private
static
Properties prop;
static
{
InputStream is =
null
;
try
{
prop =
new
Properties();
is = ClassLoader.
class
.getResourceAsStream(
"/sample.properties"
);
prop.load(is);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
public
static
String getPropertyValue(String key){
return
prop.getProperty(key);
}
public
static
void
main(String a[]){
System.out.println(
"db.host: "
+getPropertyValue(
"db.host"
));
System.out.println(
"db.user: "
+getPropertyValue(
"db.user"
));
System.out.println(
"db.password: "
+getPropertyValue(
"db.password"
));
}
}
sample.properties
1
2
3
4
db.host=appdomain.java2novice.com
db.user=java2novice
db.password=mypassword
db.service=orcl
Output:
db.host: appdomain.java2novice.comdb.user: java2novicedb.password: mypassword
0 0
- How to load Properties file from a static block or static method
- how to product static link lib .a file
- Cannot make a static reference to the non-static method setInAnimation(Animation) from the type View
- Cannot make a static reference to the non-static method getLocalActivityManager() from the type Acti
- Non-static method cannot be referenced from a static context
- How to make a direct download link to a static file
- how-to-load-resource-from-jar-file-packaged-in-a-war-file
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- How To Load CLOB Data from a File into a CLOB column using PL/SQL
- How to add or remove a static ARP entry on Linux
- How to make Universal Static library (.a file) in iOs using xCode
- Partial mock local private method or public method in the class and suppress static initial block
- When to use static method in a java class
- How to Load Dialog From a Resource DLL?
- How to remove a block from the particular page?
- How to remove a line from a file in java?
- myeclipse从数据库反射生成实体类和映射文件
- Android添加快捷方式
- 批处理创建cocos
- Tomcat中使用JNDI引用外部资源
- Ubuntu下无法联网的一种解决办法
- How to load Properties file from a static block or static method
- 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件
- python数字图像处理(3):图像像素的访问与裁剪
- git 使用场景总结
- bzoj2809: [Apio2012]dispatching
- Ubuntu 15.04 安装apache2+mysq+php5
- 当更改了JDK版本之后JSP项目和Java项目在eclipse中如何更改到当前的JDK的方法
- 【Western Digital】三角板自校准标准流程
- Handler的机制和例子二