posts - 60,comments - 71,trackbacks - 0

    JAVA中用到资源文件的情况非常多,所以开发中经常会遇到读取.properties文件的情况,现作些总结:

1.使用java.util.Properties类的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2.使用java.util.ResourceBundle类的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3.使用java.util.PropertyResourceBundle类的构造函数
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4.使用class变量的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

7.Servlet中可以用javax.servlet.ServletContext 的getResourceAsStream()方法

示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

8.用Thread当前线程方法获取
示例:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));

posted on 2008-05-27 16:06 henry1451 阅读(298) 评论(0)  编辑  收藏 所属分类: Java技术

只有注册用户登录后才能发表评论。


网站导航: