java项目中配置文件的使用

读写xml、properties 文件类型

新建java工程Test项目,项目src下建立demo.propertiesFileDemo.java、 demo.xmlFileDemo.java

引入相关jar包:


:commons-collections-3.2.jar、commons-configuration-1.6.jar、commons-lang-2.4.jar和commons-logging-1.2.jar。

开始使用的是lang3 的jar 包,configurationException相关报错
后来查了一下居然是因为这个类里面定义的configurationException是继承了lang里面的NestableException
要同时引入两个包才能使用这个类。貌似apache很多jar包都是相互牵连,用一个功能就得加载好几个包
由于lang3中已经没有NestableException这个异常类了,所以使用configuration会出现异常,所以改用lang2.6问题就解决了

1.xml Demo

1.1 xml.java

package demo;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class xmlLoadDemo {
    public static void main(String[] args){

            try {
                Configuration cfg = new XMLConfiguration("demo/config.xml");// 文件路径
                String name = cfg.getString("Account.name");
                System.out.println("name:"+ name);
            } catch (ConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }
}


1.2 config.xml

<?xml version="1.0" encoding="gbk"?>

    <Accounts>
        <Account type="by0003">
            <code>100001</code>
            <pass>123</pass>
            <name>李四</name>
            <money>1000000.00</money>
        </Account>
    </Accounts>

2. properties demo

2.1 properties java

package demo;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class PropertiesFileDemo {
    public static void main(String[] args) {
    
        try {            
            Configuration cfg = new PropertiesConfiguration("demo/config.properties");
            String name = cfg.getString("name");
            System.out.println("name:"+ name);
            
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  }     
}


2.2 config.properties

threads.max=50threas.min=2  
timout=15.52  
interactive=true  
color=red  
speed=50  
name=Default User       


需要注意的是hetClassLoader().getResourceAsStream()的参数是项目根目录下的路径,尽管config.properties是该该类文件在相同的目录下,但是不能写成getClassLoader().getResourceAsStream("config.properties"),这样程序会报错,得到的InputStream是null值。
ClassLoader()和URLClassLoader()区别:ClassLoader()只能查找src目录下的文件,而URLClassLoader()则能查找任意目录下的文件。


3. java.until.properities 类使用

public class PropertiesFileDemo {
    public static void main(String[] args) {
    
        PropertiesFileDemo pfd = new PropertiesFileDemo();        
        pfd.getcfgFromUntilProperties();
    }
    
    public void getcfgFromUntilProperties(){
    //     PropertiesTest pt = new PropertiesTest();  

            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("demo/config.properties");  
            
            System.out.println("begin!!!");  
            Properties properties = new Properties();  
            
            try{  
                properties.load(inputStream);  
                
                //  properties.load(new FileInputStream("D:\\a.ini")); // 加载硬盘地址配置文件                            
                
            }catch (IOException ioE){  
                ioE.printStackTrace();  
            }finally{  
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
            }  
            System.out.println("name:"+properties.getProperty("name"));  
        
        
    }
}


猜你喜欢

转载自blog.csdn.net/guoanddong/article/details/66969010
今日推荐