日期:2014-05-20  浏览次数:20889 次

使用J2SE读取Properties文件的六种方式(转)

使用J2SE读取Properties文件的六种方式:
1.使用java.util.Properties类的load()方法:

Java代码 复制代码
  1. InputStream?in?=?new?BufferedInputStream(new?FileInputStream(FILENAME)); ??
  2. Properties?p?=?new?Properties(); ??
  3. p.load(in);??
InputStream in = new BufferedInputStream(new FileInputStream(FILENAME));
Properties p = new Properties();
p.load(in);


2.使用java.util.ResourceBunld类的getBundle()方法:

Java代码 复制代码
  1. ResourceBundle?rb?=?ResourceBundle.ResourceBundle.getBundle(FILENAME,Locale.getDefault())??
ResourceBundle rb = ResourceBundle.ResourceBundle.getBundle(FILENAME,Locale.getDefault())


3.使用PropertyResourceBundle

Java代码 复制代码
  1. InputStream?in?=?new?BufferedInputStream(new?FileInputStream(name)); ??
  2. ResourceBundle?rb?=?new?PropertyResourceBundle(in);??
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);


4.使用Class类的getResourceAsStream(String name)
5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
下面转自:http://blog.csdn.net/bincavin/archive/2010/03/09/5359047.aspx
通常,在实际项目中,为了便于配置管理,我们需要将一些配置管理信息存储在.properties文件中,然后直接从中读取,这样就避免了很多硬编码。下面,通过一个例子,详细讲解如何读取.properties文件的数据。

  1.建立一个包config,专门存放.properties等配置文件,在config包下建一个文件a.properties,为了便于测试,在a.properties下添加如下信息:

  name=kaka

  age=28

  2.建立一个包prop,用来测试。在prop包下建立LoadProp.java文件。

  3.有很多方法来读取.properties文件,现将主要方法罗列出来:

  a.通过class的getResourceAsStream()方法来读取

  

Java代码 复制代码
  1. package?prop; ??
  2. ??
  3.   import?java.io.IOException; ??
  4. ??
  5.   import?java.io.InputStream; ??
  6. ??
  7.   import?java.util.Properties; ??
  8. ??
  9.   public?class?LoadProp?{ ??
  10. ??
  11.   public?static?void?main(String[]?args)?{ ??
  12. ??
  13.   LoadProp?loadProp?=?new?LoadProp(); ??
  14. ??
  15.   InputStream?in?=?loadProp.getClass().getResourceAsStream("/config/a.properties"); ??
  16. ??
  17.   Properties?prop?=?new?Properties(); ??
  18. ??
  19.   try?{ ??
  20. ??
  21.   prop.load(in); ??
  22. ??
  23.   }?catch?(IOException?e)?{ ??
  24. ??
  25.   e.printStackTrace(); ??
  26. ??
  27.   } ??
  28. ??
  29.   System.out.println(prop.getProperty("name",?"none")); ??
  30. ??
  31.   System.out.println(prop.getProperty("age",?"none")); ??
  32. ??
  33.   } ??
  34. ??
  35.   }??
package prop;

  import java.io.IOException;

  import java.io.InputStream;

  import java.util.Properties;

  public class LoadProp {

  public static void main(String[] args) {

  LoadProp loadProp = new LoadProp();

  InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties");

  Properties prop = new Properties();

  try {

  prop.load(in);

  } catch (IOException e) {

  e.printStackTrace();

  }

  System.out.println(prop.getProperty("name", "none"));

  System.out.println(prop.getProperty("age", "none"));

  }

  }


  一定要注意的是,class里的getResourceAsStream()方法里参数的类路径一定要在前面加上"/",否则会报错

b.使用class的getClassLoader()方法所得的ClassLoader的getResourceAsStream()来读取 package prop;

  

Java代码 复制代码
  1. import?java.io.IOException; ??
  2. ??
  3.   import?java.io.InputStream; ??
  4. ??
  5.   import?java.util.Properties; ??
  6. ??
  7.   public?class?LoadProp?{ ??
  8. ??
  9.   public?static?void?main(String[]?args)?{ ??
  10. ??
  11.   LoadProp?loadProp?=?new?LoadProp(); ??
  12. ??
  13.   InputStream?in?=?loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties"); ??
  14. ??
  15.   Properties?prop?=?new?Properties(); ??
  16. ??
  17.   try?{ ??
  18. ??
  19.   prop.load(in); ??
  20. ??
  21.   }?catch?(IOException?e)?{ ??
  22. ??
  23.   e.printStackTrace(); ??
  24. ??
  25.   } ??
  26. ??
  27.   System.out.println(prop.getProperty("name",?"none")); ??
  28. ??
  29.   System.out.println(prop.getProperty("age",?"none")); ??
  30. ??
  31.   } ??
  32. ??
  33.   }??
import java.io.IOException;

  import java.io.InputStream;

  import java.util.Properties;

  public class LoadProp {

  public static void main(String[] args) {

  LoadProp loadProp = new LoadProp();

  InputStream in = loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties");

  Properties prop = new Properties();

  try {

  prop.load(in);

  } catch (IOException e) {

  e.printStackTrace();

  }

  System.out.println(prop.getProperty("name", "none"));

  System.out.println(prop.getProperty("age", "none"));

  }

  }


  ClassLoader的getResourceAsStream()方法与Class的getResourceAsStream()方法有点区别,在这里一定不要在类路径前面加上"/",否则会报错,是不是很奇怪。

  c.使用ResourceBundle来读取

  

Java代码 复制代码
  1. package?prop; ??
  2. ??
  3.   import?java.util.ResourceBundle; ??
  4. ??
  5.   public?class?LoadProp?{ ??
  6. ??
  7.   public?static?void?main(String[]?args)?{ ??
  8. ??
  9.   ResourceBundle?rb?=?ResourceBundle.getBundle("config/a"); ??
  10. ??
  11.   System.out.println(rb.getString("name")); ??
  12. ??
  13.   System.out.println(rb.getString("age")); ??
  14. ??
  15.   } ??
  16. ??
  17.   }??
package prop;

  import java.util.ResourceBundle;

  public class LoadProp {

  public static void main(String[] args) {

  ResourceBundle rb = ResourceBundle.getBundle("config/a");

  System.out.println(rb.getString("name"));

  System.out.println(rb.getString("age"));

  }

  }

  注意,getBundle()方法里的参数,是baseName,不要把后缀名写出来,并且不要加"/"。

  好了,这是读取.properties文件的几种主要方法,还有其他的方法,基本上都大同小异。另外,在Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法来读取.properties文件,这里就不详述了。