Java 读取properties配置文件
来源:互联网 发布:刘慈欣光荣与梦想知乎 编辑:程序博客网 时间:2024/05/29 03:15
package util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
**/
public class Constants extends Properties{
private static final long serialVersionUID = 1L;
private static Properties p = null;
static {
try {
p = new Properties();
//这种方式是读jar包中的配置文件
//InputStream fis = Constants.class.getClassLoader().getResourceAsStream("../resource/config.properties");
InputStream fis = Constants.class.getResourceAsStream("../config.properties");
try {
p.load(fis);
} catch (final IOException e) {
e.printStackTrace();
throw new NullPointerException("Failed to load config file: "
+ "config.properties" + ", error: " + e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (final IOException e) {
// do nothing
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String DRIVER = p.getProperty("DRIVER") ;
public static String DRIVER_URL = p.getProperty("DRIVER_URL") ;
public static void main(String[] args){
System.out.println(DRIVER);
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
**/
public class Constants extends Properties{
private static final long serialVersionUID = 1L;
private static Properties p = null;
static {
try {
p = new Properties();
//这种方式是读jar包中的配置文件
//InputStream fis = Constants.class.getClassLoader().getResourceAsStream("../resource/config.properties");
InputStream fis = Constants.class.getResourceAsStream("../config.properties");
try {
p.load(fis);
} catch (final IOException e) {
e.printStackTrace();
throw new NullPointerException("Failed to load config file: "
+ "config.properties" + ", error: " + e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (final IOException e) {
// do nothing
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String DRIVER = p.getProperty("DRIVER") ;
public static String DRIVER_URL = p.getProperty("DRIVER_URL") ;
public static void main(String[] args){
System.out.println(DRIVER);
}
}
0 0
- java 读取properties配置文件
- Java读取properties配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java 读取*.properties配置文件
- Java读取properties配置文件
- java 读取配置文件(properties)
- java读取 .properties配置文件
- java读取properties配置文件
- Java读取properties配置文件
- Java读取properties配置文件
- java读取配置文件properties
- java读取properties配置文件
- java读取properties配置文件
- java读取properties配置文件
- java读取properties配置文件
- Java读取Properties配置文件
- java读取.properties配置文件
- 设置eclipse的debug提醒
- tomcatec出错-Web app root system property already set to different value
- 晶格振动
- [LeetCode-82] Remove Duplicates from Sorted List II
- 高并发memecache 学习视频
- Java 读取properties配置文件
- 【OCJP】 第3题---类变量的初始化
- Google Guava Collections 使用介绍
- Ubuntu 安装磁盘分区及启动项添加
- AFX_MANAGE_STATE(AfxGetStaticModuleState())DLL资源
- HVM,Hexagon处理器的虚拟器解决方案
- 优秀程序员的18大法则 -- The Principles of Good Programming
- IOS 获得当前系统时间(当前时区)
- Android中的异常