java Property

 

Properties 继承于Hashtable

他可用流做参数 FileInputStream 和 FileOutputStream

常用方法:load、store、getProperty、setProperty

 

以下为实例代码:

 

package dirk.property;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

/**
 * @author logichina
 *
 */
/**
 * @author logichina
 *
 */
public class ReadProperty {
	/*public static void main(String[] args)
	{
		Properties pps = System.getProperties();
		pps.list(System.out);
	}*/
	public static void main(String[] args)
	{
		Properties pps = new Properties();
		try
		{
			pps.load(new FileInputStream(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties"));
			//Enumeration<Object> enum1 = pps.propertyNames();
			
			Iterator<Entry<Object, Object>> ppsi = pps.entrySet().iterator();
			while(ppsi.hasNext())
			{
				Entry<Object,Object> e = ppsi.next();
				String strKey = (String)e.getKey();
				String strValue = (String)e.getValue();
				System.out.println(strKey+":"+strValue+"--");
			}
			pps.setProperty("new info","chenchen");
			pps.setProperty("dirk.team", "dallasForever");
			//writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.information","context","dirk.zhang modify");
			//writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.boss","still ku.ban","dirk.zhang modify");
			pps.store(new FileOutputStream(System.getProperty("user.dir")+"/src/test.out"), "new dirk information");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

 

以下是封装后的实例代码

 

package dirk.property;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

/**
 * @author logichina
 *
 */
/**
 * @author logichina
 *
 */
public class ReadProperty {
	/*public static void main(String[] args)
	{
		Properties pps = System.getProperties();
		pps.list(System.out);
	}*/
	public static void main(String[] args)
	{
		Properties pps = new Properties();
		try
		{
			pps.load(new FileInputStream(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties"));
			//Enumeration<Object> enum1 = pps.propertyNames();
			
			Iterator<Entry<Object, Object>> ppsi = pps.entrySet().iterator();
			while(ppsi.hasNext())
			{
				Entry<Object,Object> e = ppsi.next();
				String strKey = (String)e.getKey();
				String strValue = (String)e.getValue();
				System.out.println(strKey+":"+strValue+"--");
			}
			pps.setProperty("new info","chenchen");
			pps.setProperty("dirk.team", "dallasForever");
			writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.information","context","dirk.zhang modify");
			writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.boss","still ku.ban","dirk.zhang modify");
			//pps.store(new FileOutputStream(System.getProperty("user.dir")+"/src/test.out"), "new dirk information");
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void readPropertyToObj(String path,PropertyInfo infoObj) throws FileNotFoundException, IOException
	{
		Properties pps = new Properties();
		pps.load(new FileInputStream(path));
		/*封装对象*/
		infoObj.setInfo(pps);
	}
	
	public static Properties readPropertyToPps(String path) throws FileNotFoundException, IOException
	{
		Properties pps = new Properties();
		pps.load(new FileInputStream(path));
		return pps;
	}
	public static void writeProperty(String path,String name,String value,String note) throws FileNotFoundException, IOException
	{
		Properties pps = new Properties();
		pps.load(new FileInputStream(path));
		pps.setProperty(name, value);
		pps.store(new FileOutputStream(path), note);
	}
	
	
	
	/**
	 * property obj for dirk
	 * @author dirk.zhang
	 */
	class DirkObj implements PropertyInfo
	{
		public final String NAME="name";
		public final String NUM="num";
		public final String BOSS="boss";
		private String name="";
		private String num="";
		private String boss="";
		
		public String setInfo(Properties pps) {
			// TODO Auto-generated method stub
			if(pps==null)
				return "error no such property obj";
			name = pps.getProperty(NAME);
			num = pps.getProperty(NUM);
			boss = pps.getProperty(BOSS);
			boss = pps.getProperty("没有");
			return "";
		}
		
		public String getName() {
			return name;
		}


		public void setName(String name) {
			this.name = name;
		}


		public String getNum() {
			return num;
		}


		public void setNum(String num) {
			this.num = num;
		}


		public String getBoss() {
			return boss;
		}


		public void setBoss(String boss) {
			this.boss = boss;
		}
		
		@Override
		public String toString()
		{
			//System.out.println(this.name+":"+this.num+" "+this.boss);
			return this.name+":"+this.num+" "+this.boss;
		}
	}

}

 还定义一个文件和bean交互的接口

 

package dirk.property;

import java.util.Properties;


/**
 * property obj interface
 * @author dirk.zhang
 */
public interface PropertyInfo {
	String setInfo(Properties pps);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值