Java之Properties类【整理】

一、Java Properties类

 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。在Java中,其配置文件常为.properties文件格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

Properties类继承自Hashtable,如下:

它提供了几个主要的方法:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。


使用J2SE API读取Properties文件的六种方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = new 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);

补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

<span style="color:#333333;">//关于Properties类常用的操作
public class TestProperties {
    //根据Key读取Value
    public static String GetValueByKey(String filePath, String key) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
            pps.load(in);
            String value = pps.getProperty(key);
            System.out.println(key + " = " + value);
            return value;
            
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    //读取Properties的全部信息
    public static void GetAllProperties(String filePath) throws IOException {
        Properties pps = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in);
        Enumeration en = pps.propertyNames(); //得到配置文件的名字
        
        while(en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }
        
    }
    
    //写入Properties信息
    public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
        Properties pps = new Properties();
        
        InputStream in = new FileInputStream(filePath);
        //从输入流中读取属性列表(键和元素对) 
        pps.load(in);
        //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  
        //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStream out = new FileOutputStream(filePath);
        pps.setProperty(pKey, pValue);
        //以适合使用 load 方法加载到 Properties 表中的格式,  
        //将此 Properties 表中的属性列表(键和元素对)写入输出流  
        pps.store(out, "Update " + pKey + " name");
    }
    
    public static void main(String [] args) throws IOException{
        //String value = GetValueByKey("Test.properties", "name");
        //System.out.println(value);
        //GetAllProperties("Test.properties");
        WriteProperties("Test.properties","long", "212");
    }
}</span><span style="color:#3366ff;">
</span>

public class ReadProperties {
    static private String driver = null;
    static private String url = null;
    static private String user = null;
    static private String password = null;
    
    static{
        loads();
    }

    synchronized static public void loads(){
        if(driver == null || url == null || user == null || password == null){
            InputStream is = ReadProperties.class.getResourceAsStream("/db.properties");
            Properties dbproperties = new Properties();
            try {
                dbproperties.load(is);
                        
                driver = dbproperties.getProperty("driver").toString();
                url = dbproperties.getProperty("url").toString();  
                user = dbproperties.getProperty("user").toString();  
                password = dbproperties.getProperty("password").toString(); 
                    
            }
            catch (Exception e) {
                System.err.println("不能读取属性文件. " + "请确保db.properties在CLASSPATH指定的路径中");
            }
        }
    }    
    public static String getDriver() {
        if(driver==null)
            loads();
        return driver;
    }    
    public static String getUrl() {
        if(url==null)
            loads();
        return url;
    }    
    public static String getUser() {
        if(user==null)
            loads();
        return user;
    }     
    public static String getPassword() {
        if(password==null)
            loads();
        return password;
    }  
}
这个方法不仅能够缓存配置文件内容,还能够做到自动加载配置文件的内容到内存,使用者完全不用考虑手动加载的过程,只需要在需要用到的地方直接调用ReadProperties的get方法[例:String url = ReadProperties.getgetUrl()]就可以了(因为是静态方法,不用创建对象),这样如果内存中有缓存,函数就会直接读取内存中的数据,节省时间,如果没有缓存系统也会自动加载!!!
FileInputStream is = new FileInputStream(filepath); 
Properties dbproperties = new Properties();
dbproperties.load(is);
当采用绝对定位的时候,如果将工程移到另外一个盘符下运行,就需要修改源代码,否则就会报错,
可是如果使用相对路径,当ReadPorperties类移到另外一个包中时,还是要修改源代码,否则会报错

InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbproperties = new Properties();
dbproperties.load(is);
方法的好处就是不用指出配置文件的绝对路径,而且不管是将ReadPorperties类放到另外的包中,
还是将整个工程移到到另外的盘符下,代码依然可以正常运行,不会有找不到文件的问题,
props.load(ReadProperties.class.getClassLoader().getResourceAsStream(filename));意思是获得从Properties类获得类加载器(类加载器主要有四种,分别加载不同类型的类,加载只是把class文件放进内存,并没有产生对象),并把指定文件转化为流。这一步,有很多新手,直接往load()里填文件名或具体文件路径名,程序运行时会报错找不到指定路径。所以,一定要注意这点。

属性文件一般放在src目录下,编译后属性文件被自动放到相应的包内
读属性文件
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/IcisReport.properties");
prop.load(in);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();)
{
String key = (String) it.next();
}
------------------------
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
-----------------------------------------------------------------------------------------
Class.getResourceAsStream ("/some/pkg/resource.properties");
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
java.util.ResourceBundle rs = java.util.ResourceBundle.getBundle("some.pkg.resource");
rs.getString("xiaofei");
-----------------------------------------------------------------------------------------
写属性文件
Configuration saveCf = new Configuration();
saveCf.setValue("min", "10");
saveCf.setValue("max", "1000");
saveCf.saveFile(".\config\save.perperties","test");


总结:java的properties文件需要放到classpath下面,这样程序才能读取到,有关classpath实际上就是java类或者库的存放路径,

在java工程中,properties放到class文件一块。

在web应用中,最简单的方法是放到web应用的WEB- INF\classes目录下即可,也可以放在其他文件夹下面,这时候需要在设置classpath环境变量的时候,

将这个文件夹路径加到 classpath变量中,这样也也可以读取到。

在此,你需要对classpath有个深刻理解,classpath绝非系统中刻意设定的那个系统环境变量,WEB-INF\classes其实也是,java工程的class文件目录也是。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值