---
title: "读取配置文件的几个用法"
author: "arison1983"
date: "2009-02-10T09:25:54Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-02-10T09:29:13Z"
id: 325971
url: "https://arison1983.iteye.com/blog/325971"
---

```
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....
```