zoukankan      html  css  js  c++  java
  • 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("-------------------------");
            }
        }
    }
  • 相关阅读:
    文档中心 FetchURL Sina App Engine
    GroovyHelp使用指南 蛟龍居 BlogJava
    北京个人ADSL和企业ADSL有什么区别啊?
    手动编译cppserv0.1.99 @ apache2.2
    漫谈Web Service工作原理及.NET平台的实现机制
    Servlet与模板模式那些事
    [跟着hsp步步学习系统]oracle培训学习集锦全360度扫描(5)
    诺基亚死于大数据时代 上海唯一旗舰店关门
    苹果iPad强于分销渠道:平板领域将继续强势
    用友放弃野蛮生长战略 转型平台软件商
  • 原文地址:https://www.cnblogs.com/nayitian/p/2873347.html
Copyright © 2011-2022 走看看