익숙한 클래스 Properties, Enumeration, HashMap, Set 등의 기본 조종

package com.fly.Test;
/**
 * @author  sunlovefly
 * @time :2012.8
 *  , :Properties、Enumeration、HashMap、Set 
 */ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertyEditor {
	
	public static void main(String[] args)throws Exception{
		Properties prop = new Properties();//    
		prop.setProperty("name", "sunfei");
		prop.setProperty("sex", "male");
		prop.setProperty("age", "22");
		prop.setProperty("city", " ");
        FileOutputStream fos = new FileOutputStream("prop.properties");   
        // Properties , 
         prop.store(fos, "Copyright (c) sunfly");
        fos.close();//     
        
        Properties prop1 = new Properties();
        // 
        FileInputStream fis = new FileInputStream("prop.properties");
        prop1.load(fis);
        fis.close();
        
        // 
        Enumeration enume = null;
        enume = prop1.propertyNames();
        
        // 
        HashMap<String,String> map = new HashMap<String,String>();
        while(enume.hasMoreElements()){
        	String name = (String)enume.nextElement();
        	String value = prop1.getProperty(name);
        	map.put(name, value);
        	System.out.println(name+"  :  "+value);
        }
        // 
        // 1:
        for(Iterator iter = map.keySet().iterator();iter.hasNext();){
        	Object key = iter.next();
        	Object val = map.get(key);
        	System.out.println("key:"+key+"---->value:"+val);
        }
        // 2:
        for(Iterator iter = map.entrySet().iterator();iter.hasNext();){
        	Map.Entry entry = (Map.Entry)iter.next();
        	Object key = entry.getKey();
        	Object val = entry.getValue();
        	System.out.println("key:"+key+"---->value:"+val);
        }
        
        // 
        Set<Object> set = new HashSet<Object>();
        set = prop1.keySet();
        for(Object obj:set){
        	String key = (String)obj;
        	String val = prop1.getProperty(key);
        	System.out.println("key:"+(String)obj+"  value:"+val);
        }
	}

}

좋은 웹페이지 즐겨찾기