首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Properties 种读取配置文件

2012-11-09 
Properties 类读取配置文件1、使用java.util.Properties类的load()方法 示例:  Java代码  InputStream in

Properties 类读取配置文件

1、使用java.util.Properties类的load()方法 示例:

  Java代码

  InputStream in = lnew BufferedInputStream(new FileInputStream(name));

  Properties p = new Properties();

  p.load(in);

  2、使用java.util.ResourceBundle类的getBundle()方法

  示例:

  Java代码

  ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

  用ResourceBundle读取.properties文件可避免路径问题

  我在jar里读取.properties文件时,总是找不到文件路径,后来用ResourceBundle读取.properties文件即可避免路径问题,代码如下:

  //process为文件名,切记不要加 .properties, URL是文件里的键名

  Java代码

  ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");

  String s = bundle.getString("URL");

  System.out.println(s);

  pURL = s;

  3、使用java.util.PropertyResourceBundle类的构造函数

  示例:

  Java代码

  InputStream in = new BufferedInputStream(new FileInputStream(name));

  ResourceBundle rb = new PropertyResourceBundle(in);

  4、使用class变量的getResourceAsStream()方法

  示例:

  Java代码

  InputStream in = 类名.class.getResourceAsStream(name);

  Properties p = new Properties();

  p.load(in);

  5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例:

  Java代码

  InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);

  Properties p = new Properties();

  p.load(in);

  6、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例:

  Java代码

  InputStream in = ClassLoader.getSystemResourceAsStream(name);

  Properties p = new Properties();

  p.load(in);

  7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:

  Java代码

  InputStream in = context.getResourceAsStream(path);

  Properties p = new Properties();

  p.load(in);

?

?

================================================================================

    import?java.io.IOException; ??import?java.io.InputStream; ??import?java.util.Enumeration; ??import?java.util.Properties; ??????/** ??*??Properties? ??*??配置文件要有中文就使用xml文件加载 ??*?@author?whp ??* ??*/??public?class?PropertiesConfig?extends?Properties?{ ??????/** ??????*? ??????*/??????private?static?final?long?serialVersionUID?=?1L; ?????? ??????protected?static?final?Properties?p=new?Properties(); ??????public?PropertiesConfig(String?file)?{ ??????????InputStream?in=PropertiesConfig.class.getResourceAsStream(file); ??????????if(in!=null){ ??????????????try?{ ??????????????????p.load(in); ??????????????????in.close(); ??????????????}?catch?(IOException?e)?{ ??????????????????System.out.println("加载配置文件错误"); ??????????????????e.printStackTrace(); ??????????????} ?????????????? ??????????} ??????} ??????@Override??????public??String?getProperty(String?key,?String?defaultValue)?{ ??????????return?p.getProperty(key,?defaultValue); ??????} ??????@Override??????public?String?getProperty(String?key)?{ ??????????return?p.getProperty(key); ??????} ?????? ??????/** ??????*??属性列表 ??????*/??????private?void?propertiesList(){ ??????????Enumeration?e=p.keys(); ??????????while(e.hasMoreElements()){ ??????????????String?key=(String)e.nextElement(); ??????????????System.out.println(key+"="+p.getProperty(key)); ??????????} ??????} ?????? ??????public?static?void?main(String[]?args)?{ ??????????PropertiesConfig?pro=new?PropertiesConfig("config.properties"); ??????????//String?s=pro.getProperty("name"); ??????????pro.propertiesList(); ??????????//System.out.println(s); ??????} ?????? ?????? ????}??

热点排行