Java:IO/NIO篇,读写属性文件(properties)

1. 描述

尝试用多种方法读取属性文件。

  1. 测试打印系统属性;
  2. 测试读取、写入用户属性文件;
  3. 测试读取类库中的属性文件。

2. 示范代码

package com.clzhang.sample.io;

import java.io.*;   
import java.util.*; 

import org.junit.Test;

/**
 * 属性文件测试类,
 * 1.测试打印系统属性;
 * 2.测试读取、写入用户属性文件;
 * 3.测试读取类库中的属性文件。
 * @author Administrator
 *
 */
public class PropertyTest {
    @SuppressWarnings("rawtypes")
    @Test
    public void testProp() throws Exception {
        // 打印系统属性
        Properties propSystem = System.getProperties();   
        System.out.println("-------------------------");   
        for (Enumeration e = propSystem.propertyNames(); e.hasMoreElements();) {   
            String key = (String) e.nextElement();   
            System.out.println(key + "=" + propSystem.getProperty(key));   
        }   
        System.out.println("-------------------------");
        
        // 方式一,硬编码指定属性文件位置
//        String filename = "C:\\solr\\collection1\\conf\\prop.properties"; 
        
        // 方式二,相对路径指定属性文件
        String filename = "prop.properties";
        File file = new File(filename);
        if(!file.exists()) {
            System.out.println("在用户默认目录:" + propSystem.get("user.dir") + "下面找不到:" + file.getName() + "文件!");
        }else {
            // 读取属性配置文件
            Properties prop = new Properties();  
            FileInputStream fis = new FileInputStream(file);
            prop.load(fis);   
            fis.close();   
            
            // 读取属性值
            System.out.println("log4j.appender.R=" + prop.getProperty("log4j.appender.R"));   
            System.out.println("does_not_exist_node=" + prop.getProperty("dose_not_exist_node", "Hello"));   
       
            // 更改属性值
            prop.setProperty("log4j.appender.R", "你想怎样"); 
            prop.setProperty("add_node", "Hello There!");   
    
            // 保存到文件
            FileOutputStream fos = new FileOutputStream(filename);   
            prop.store(fos, "a description of the property list");   
            fos.close();   
            System.out.println("-------------------------");
        }
        
        // 方式三,系统类库中查找属性文件
        String propFileInJar = "com/clzhang/sample/io/prop.properties";
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(propFileInJar);
        // 上面二行代码等同于下面二行任一行代码
        // 相对路径
//      InputStream is = com.clzhang.sample.io.PropertyTest.class.getResourceAsStream("prop.properties");
        // 绝对路径
//      InputStream is = com.clzhang.sample.io.PropertyTest.class.getResourceAsStream("/com/clzhang/sample/io/prop.properties");
        if(is == null) {
            System.out.println("在系统类库中没有找到:" + propFileInJar + "文件!");
            System.out.println("类路径:" + propSystem.get("java.class.path"));
        }else {
            Properties prop = new Properties(); 
            prop.load(is);   
            is.close();   
            
            System.out.println("log4j.appender.R=" + prop.getProperty("log4j.appender.R"));   
            System.out.println("-------------------------");
        }
    }
}

 

posted @ 2013-01-23 16:15  那些年的事儿  阅读(850)  评论(0编辑  收藏  举报