日期:2014-05-19  浏览次数:20651 次

关于properties文件
在开发web系统时,一直不理解这种文件的加载方法,请各位大神踊跃赐教!!!
学习三大框架的时候,用的都是xml文件的加载,在web.xml里面会有加载路径,比如spring的配置文件,会有节点<param-value></param-value>里面填写spring配置文件的路径,今天看师兄的项目,里面有几个properties文件 ,但是我找不到 体现加载路径的文件  
请各位说一下哈
------最佳解决方案--------------------
propries 是一种字典结构,加载主要有六种方法可以加载,都是通过

使用J2SE API读取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);

------其他解决方案--------------------
properties配置文件是在开发中需要的时候才获取的,一般中专门写个类出来获取,以下是我的一个例子:


public class SiteUrl {
private static Properties prop = new Properties();
static {

try {
prop.load(SiteUrl.class.getClassLoader().getResourceAsStream(
"siteUrl.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}

public static String readUrl(String key) {
return (String) prop.get(key);

}
}

------其他解决方案--------------------
详情请参见util包的Properties类
------其他解决方案--------------------
xml中没有那么一定在java类中。
------其他解决方案--------------------
引用:
properties配置文件是在开发中需要的时候才获取的,一般中专门写个类出来获取,以下是我的一个例子:



Java code?



123456789101112131415161718

public class SiteUrl {     private static Properties prop = new Properties();     static { ……


+1,
properties适用于比较简单的key-value这种一对一的结构,
xml适用于复杂的树形结构。