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

自写的Properties的toString方法,去空格,propertiestostring

来源: javaer 分享于  点击 8570 次 点评:67

自写的Properties的toString方法,去空格,propertiestostring


/** 自己写的Properties的toString方法
	 * Properties自身的toString方法返回的字串逗号前会有空格
	 * @param prop a->1,b->2,c->3
	 * @return a=1,b=2,c=3
	 * @author ferndean
	 */
	public String propToString(Properties prop)
	{
		StringBuffer buff = new StringBuffer();
		Enumeration<?> e = prop.propertyNames();
		while (e.hasMoreElements())
		{
			String key = (String) e.nextElement();
			String value = prop.getProperty(key);
			buff.append(key).append("=").append(value).append(",");
		}
		buff.delete(buff.length() - 1, buff.length());
		return buff.toString();
	}


上面的方法,一般是自定义多个扩展字段放入Properties中存入数据库,那么相反从数据库扩展字段取出Properties对象,怎么取呢,看下面代码:

/**
	 * 从数据库中读取扩展字段返回枚举对象
	 * 
	 * @param str
	 * @return
	 */
	public Properties getPropObj(String str)
	{
		String[] strs = str.split("\n");

		Properties prop = new Properties();

		String name = "";
		String value = "";
		for (int i = 0; i < str.length(); i++)
		{
			int equalIdx = strs[i].indexOf("=");

			if (equalIdx != -1)
			{
				name = strs[i].substring(0, equalIdx).trim();
				value = strs[i].substring(equalIdx + 1);

				prop.setProperty(name, value);
			}
		}
		return prop;
	}


相关文章

    暂无相关文章

用户点评