ITeye博客

读取配置文件的几个用法

arison1983 · 2009-02-10 09:25 · java 基础 · Markdown

private static final String    THIS_PROPERTIES    = "global";
//这个文件的全名 是 global.properties , 扩展名貌似可以省略...
ResourceBundle    rb        =  ResourceBundle.getBundle(THIS_PROPERTIES);
    Enumeration        enu      =  rb.getKeys();
    HashMap            FIELDS   =  new HashMap();
    while (enu.hasMoreElements()) {
        sKey    = (String) enu.nextElement();
        sVal    = rb.getString(sKey);
        FIELDS.put(sKey, sVal);
    }

 

//读取其他文件的时候也可以这样,但是格式必须是key = value 这种的
String  properyFilename = "test.ini";		

Properties properties = new Properties();
InputStream inputStream = null;
	try {
		if (!(new File(properyFilename).exists()))
				return ;
		inputStream = new BufferedInputStream(new FileInputStream(properyFilename));
		properties.load(inputStream);
		inputStream.close();
	} catch (FileNotFoundException e) {
			e.printStackTrace();
	} catch (IOException e) {
			e.printStackTrace();
	}

//load之后 就可以从中读取指定参数了
//如
properites.getProperty("userName").trim();
//etc....

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。