读取配置文件的几个用法

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

你可能感兴趣的:(java,基础,java,基础-读取配置文件的几个用法)