java 从属性文件加载数据的方法工具类 实例 可直接使用
2013-08-27 21:03
826 查看
package com.alik.ecs.config;
import java.io.InputStream;
import java.util.Properties;
public class ConfigPropertiesUtil {
private static Properties prop = null;
//加载配置文件
static{
try {
loadProperties();
loadProperties2();
} catch (Exception e) {
e.printStackTrace();
}
}
//无参构造方法
public ConfigPropertiesUtil() {
}
//加载配置文件方法
public static void loadProperties() throws Exception {
if (prop != null)
return;
prop = new Properties();
InputStream in = null;
try {
in = ConfigPropertiesUtil.class.getResourceAsStream("/source.properties");
prop.load(in);
} catch (Exception e) {
throw new Exception("[加载配置文件出错]", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new Exception("[关闭加载配置文件输入流出错]", e);
}
}
}
//加载配置文件方法
public static void loadProperties2() throws Exception {
if (prop != null)
return;
prop = new Properties();
InputStream in = null;
try {
in = ConfigPropertiesUtil.class.getResourceAsStream("/allusers.properties");
prop.load(in);
} catch (Exception e) {
throw new Exception("[加载配置文件出错]", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new Exception("[关闭加载配置文件输入流出错]", e);
}
}
}
//根据key获取value值
public static String getProperty(String key) {
return prop.getProperty(key);
}
public static Properties getProperty() {
return prop;
}
public static String getProperty(String key, String defaultValue) {
String value = prop.getProperty(key);
if (value == null)
return defaultValue;
return value.trim();
}
public static void setProperty(String key, String value) {
prop.setProperty(key, value);
}
}
import java.io.InputStream;
import java.util.Properties;
public class ConfigPropertiesUtil {
private static Properties prop = null;
//加载配置文件
static{
try {
loadProperties();
loadProperties2();
} catch (Exception e) {
e.printStackTrace();
}
}
//无参构造方法
public ConfigPropertiesUtil() {
}
//加载配置文件方法
public static void loadProperties() throws Exception {
if (prop != null)
return;
prop = new Properties();
InputStream in = null;
try {
in = ConfigPropertiesUtil.class.getResourceAsStream("/source.properties");
prop.load(in);
} catch (Exception e) {
throw new Exception("[加载配置文件出错]", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new Exception("[关闭加载配置文件输入流出错]", e);
}
}
}
//加载配置文件方法
public static void loadProperties2() throws Exception {
if (prop != null)
return;
prop = new Properties();
InputStream in = null;
try {
in = ConfigPropertiesUtil.class.getResourceAsStream("/allusers.properties");
prop.load(in);
} catch (Exception e) {
throw new Exception("[加载配置文件出错]", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new Exception("[关闭加载配置文件输入流出错]", e);
}
}
}
//根据key获取value值
public static String getProperty(String key) {
return prop.getProperty(key);
}
public static Properties getProperty() {
return prop;
}
public static String getProperty(String key, String defaultValue) {
String value = prop.getProperty(key);
if (value == null)
return defaultValue;
return value.trim();
}
public static void setProperty(String key, String value) {
prop.setProperty(key, value);
}
}
相关文章推荐
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- Java 反射将配置文件数据加载到对象属性中Reflect与Properties使用
- java中使用Properties加载XML文件设置java窗体应用程序的窗体属性
- jQuery使用$.get()方法从服务器文件载入数据实例
- java修改文件名-renameTo()方法的使用实例,复制一个文件或者叫转存一个文件
- java中Properties文件加载和使用方法
- 实例展示使用Java压缩和解压缩7z文件的方法
- 使用easyui-combobox的data属性加载定义好在*.js文件中的JSON数据的问题
- 使用JAVA读写Properties属性文件的方法详解
- Spark中加载本地(或者hdfs)文件以及 spark使用SparkContext实例的textFile读取多个文件夹(嵌套)下的多个数据文件
- Java解析json文件的方法--直接拷贝可以使用
- oracle下的sqlldr命令使用方法(sqlldr工具的使用(批量将文本文件数据加载到数据表))
- Java压缩文件工具类ZipUtil使用方法代码示例
- mac下使用java命令直接执行类文件(找不到或无法加载主类)
- java加载外部文件数据到代码中:外部数据文件放到jar包中,调用方法getResourceAsStream
- java加载外部文件数据到代码中:外部数据文件放到jar包中,调用方法getResourceAsStream
- 在用java进行极光推送的时候,需要的一个工具类: 使用的时候,直接调用 sendToRegistrationId 方法
- mac 下 使用 java运行 class 文件 总是提示 “错误: 找不到或无法加载主类”的解决方法
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- 使用JAVA读写Properties属性文件的方法详解