`

在java中读取配置文件(*.properties)的方法

    博客分类:
  • J2SE
阅读更多
转载:http://www.gold98.net/blog/article.asp?id=776
package test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * @author alex  大灰狼 from 梦想博客
 *
 */

public class TestReadPropFile {
    public static void main(String[] args) {
        InputStream inputStream = null;
        try {
            String fileName = "/config.properties";
            inputStream = TestReadPropFile.class.getResourceAsStream(fileName);
            Properties prop = new Properties();
            prop.load(inputStream);
            Set keyValue = prop.keySet();
            for (Iterator it = keyValue.iterator(); it.hasNext();) {
                String key = (String) it.next();
                String value=(String) prop.get(key);
                System.out.println("key: "+key+"  value: "+value);               
            }
            System.out.println(prop.getProperty("CostExpress"));
            System.out.println(prop.getProperty("path"));
            inputStream.close();
        } catch (IOException e) {
           System.out.println(e.getStackTrace());
        }
    }
}

TestReadPropFile.class在非静态方法里可以用getClass()替换!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics