欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java使用Properties类读取配置文件信息,properties配置文件,java中使用Prope

来源: javaer 分享于  点击 44667 次 点评:153

java使用Properties类读取配置文件信息,properties配置文件,java中使用Prope


java中使用Properties类可以方便的读取配置信息。首先需要创建一个Properties类的实例,然后使用load方法在入FileInputStream实例指向的配置文件。

假定我们要读取的配置文件内容如下:

# System configuration# Comments will automatically be excluded by the program.parameter1=value1parameter2=value2

代码:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Enumeration;import java.util.Properties;/** * Main.java * * @author byrx.net */public class Main {    Properties config;    /**     * Loads configuration parameters from a textfile and print them out.     */    public void loadConfigFile() {        //Load configuration file        String filename = "conf/systemconfig.txt";        config = new Properties();        try {            config.load(new FileInputStream(filename));        } catch (FileNotFoundException ex) {            ex.printStackTrace();            return;        } catch (IOException ex) {            ex.printStackTrace();            return;        }        //Print out the configuration parameters        Enumeration en = config.keys();        System.out.println("********** System configuration **********");        while (en.hasMoreElements()) {            String key = (String) en.nextElement();            System.out.println(key + " => " + config.get(key));        }    }    /**     * Starts the program     *     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().loadConfigFile();    }}

下面是输出:

********** System configuration **********parameter2 => value2parameter1 => value1
相关栏目:

用户点评