工具(一):properties配置文件读取工具类
2017-05-26 22:05
405 查看
本文着重介绍properties配置文件的解析使用。闲言少叙,直接上工具:
package com.wdy.tools.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* Properties配置文件处理工具
* @author wdy
*/
public class PropertiesUtil {
// 静态块中不能有非静态属性,所以加static
private static Properties prop = null;
//静态块中的内容会在类别加载的时候先被执行
static {
try {
prop = new Properties();
// prop.load(new FileInputStream(new File("C:\\jdbc.properties")));
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("configs/jdbc.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//静态方法可以被类名直接调用
public static String getValue(String key) {
return prop.getProperty(key);
}
}
以上就是PropertiesUtil工具类了,具体用法:
假设你的配置文件是这样的:
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/myorcl
username = root
password = root 那么你可以这样读取你的配置信息:
String driver = PropertiesUtil.getValue("driver");
System.out.println(driver);//输出结果:com.mysql.jdbc.Driver 这样你就把你的配置信息读取成功了。是不是很简单。
package com.wdy.tools.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* Properties配置文件处理工具
* @author wdy
*/
public class PropertiesUtil {
// 静态块中不能有非静态属性,所以加static
private static Properties prop = null;
//静态块中的内容会在类别加载的时候先被执行
static {
try {
prop = new Properties();
// prop.load(new FileInputStream(new File("C:\\jdbc.properties")));
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("configs/jdbc.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//静态方法可以被类名直接调用
public static String getValue(String key) {
return prop.getProperty(key);
}
}
以上就是PropertiesUtil工具类了,具体用法:
假设你的配置文件是这样的:
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/myorcl
username = root
password = root 那么你可以这样读取你的配置信息:
String driver = PropertiesUtil.getValue("driver");
System.out.println(driver);//输出结果:com.mysql.jdbc.Driver 这样你就把你的配置信息读取成功了。是不是很简单。
相关文章推荐
- 方法:一个简单的读取配置文件.properties的工具类 JAVA
- Eclipse和Tomcat和Java读取配置文件工具类PropertiesUtil
- 自己编写的properties配置文件读取工具,备忘
- java工具类开发之日期工具类DateUtils和读取属性配置文件工具类PropertiesUtils
- java 数据库读取工具类(读取config.properties配置文件)
- 经过修改的读取配置文件PROPERTIES的小工具,对象尽量创建一次
- 读取properties配置文件的工具类
- 读取properties配置文件工具类
- java 数据库读取工具类(读取config.properties配置文件)
- 读取properties配置文件工具类
- 针对config.properties的配置文件读取的工具类
- XMLUtil工具类读取XML或properties格式的配置文件
- 一个读取配置文件的工具类写法 (properties文件和文本文件)
- java读取properties配置文件工具类
- java 数据库读取工具类(读取config.properties配置文件)
- java 数据库读取工具类(读取config.properties配置文件)[包含线程安全]
- properties配置文件的读取工具类
- properties配置文件读取工具类
- java读取项目里配置文件properties的工具
- java读取properties配置文件