赞
踩
配置文件的获取,简单的小工具
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
-
- public class PropertiesUtil {
-
- private static Properties properties;
- // 采用静态内部类构建单例模式
- private static class LazyHolder {
- private static final PropertiesUtil INSTANCE = new PropertiesUtil();
- }
- private PropertiesUtil (){}
- // 当调用该方法实例化的时候,才会创建该类,懒加载
- public static final PropertiesUtil getInstance() {
- return LazyHolder.INSTANCE;
- }
-
- static{
- properties = new Properties();
- InputStream is = null;
- // 配置文件为当前类的class文件下
- is = PropertiesUtil.class.getResourceAsStream("config.properties");
- try {
- properties.load(is);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void main(String[] args) {
- System.out.println(properties.getProperty("username"));
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。