package cn.ict.zyq.bestConf.cluster.Utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtil {

	public PropertiesUtil() {
		// TODO Auto-generated constructor stub
	}
	
	//根据Key读取Value
    public static String GetValueByKey(String filePath, String key) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
            pps.load(in);
            String value = pps.getProperty(key);
            System.out.println(key + " = " + value);
            return value;
            
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    //读取Properties的全部信?
    public static Properties GetAllProperties(String filePath) throws IOException {
        Properties pps = new Properties();
        InputStream in = new BufferedInputStream(new FileInputStream(filePath));
        pps.load(in);
/*        Enumeration en = pps.propertyNames(); //得到配置文件的名?
        
        while(en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }*/
        return pps;
    }
    
    //写入Properties信息
    public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
        Properties pps = new Properties();
        
        //make sure the file exists
        File propFile = new File(filePath);
        if(!propFile.exists())
        	propFile.createNewFile();
        
        InputStream in = new FileInputStream(filePath);
        //从输入流中读取属性列表（键和元素对） 
        pps.load(in);
        //调用 Hashtable 的方?put。使?getProperty 方法提供并行性?  
        //强制要求为属性的键和值使用字符串。返回??Hashtable 调用 put 的结果?
        OutputStream out = new FileOutputStream(filePath);
        pps.setProperty(pKey, pValue);
        //以?合使?load 方法加载?Properties 表中的格式，  
        //将此 Properties 表中的属性列表（键和元素对）写入输出? 
        pps.store(out, "Update " + pKey + " name");
    }
    
    public static void main(String [] args) throws IOException{
        //String value = GetValueByKey("Test.properties", "name");
        //System.out.println(value);
        //GetAllProperties("data/comtConfig.properties");
        WriteProperties("data/Test.properties","long", "212");
    }


}

@@ -367,69 +367,9 @@
  %7B%0A%09
-%09// TODO Auto-generated constructor stub%0A%09%7D%0A%09%0A%09//%E6%A0%B9%E6%8D%AEKey%E8%AF%BB%E5%8F%96Value
+%7D
 %0A   
@@ -867,27 +867,8 @@
     
-//%E8%AF%BB%E5%8F%96Properties%E7%9A%84%E5%85%A8%E9%83%A8%E4%BF%A1?
 %0A   
@@ -1098,294 +1098,8 @@
 n);%0A
-/*        Enumeration en = pps.propertyNames(); //%E5%BE%97%E5%88%B0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%9A%84%E5%90%8D?%0A        %0A        while(en.hasMoreElements()) %7B%0A            String strKey = (String) en.nextElement();%0A            String strValue = pps.getProperty(strKey);%0A            System.out.println(strKey + %22=%22 + strValue);%0A        %7D*/%0A
     
@@ -1129,29 +1129,8 @@
    %0A
-    //%E5%86%99%E5%85%A5Properties%E4%BF%A1%E6%81%AF%0A
     
@@ -1287,44 +1287,8 @@
    %0A
-        //make sure the file exists%0A
     
@@ -1470,159 +1470,22 @@
     
-//%E4%BB%8E%E8%BE%93%E5%85%A5%E6%B5%81%E4%B8%AD%E8%AF%BB%E5%8F%96%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8%EF%BC%88%E9%94%AE%E5%92%8C%E5%85%83%E7%B4%A0%E5%AF%B9%EF%BC%89 %0A        pps.load(in);%0A        //%E8%B0%83%E7%94%A8 Hashtable %E7%9A%84%E6%96%B9?put%E3%80%82%E4%BD%BF?getProperty %E6%96%B9%E6%B3%95%E6%8F%90%E4%BE%9B%E5%B9%B6%E8%A1%8C%E6%80%A7?  %0A        //%E5%BC%BA%E5%88%B6%E8%A6%81%E6%B1%82%E4%B8%BA%E5%B1%9E%E6%80%A7%E7%9A%84%E9%94%AE%E5%92%8C%E5%80%BC%E4%BD%BF%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E3%80%82%E8%BF%94%E5%9B%9E??Hashtable %E8%B0%83%E7%94%A8 put %E7%9A%84%E7%BB%93%E6%9E%9C?
+pps.load(in); 
 %0A   
@@ -1583,98 +1583,8 @@
 e);%0A
-        //%E4%BB%A5?%E5%90%88%E4%BD%BF?load %E6%96%B9%E6%B3%95%E5%8A%A0%E8%BD%BD?Properties %E8%A1%A8%E4%B8%AD%E7%9A%84%E6%A0%BC%E5%BC%8F%EF%BC%8C  %0A        //%E5%B0%86%E6%AD%A4 Properties %E8%A1%A8%E4%B8%AD%E7%9A%84%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8%EF%BC%88%E9%94%AE%E5%92%8C%E5%85%83%E7%B4%A0%E5%AF%B9%EF%BC%89%E5%86%99%E5%85%A5%E8%BE%93%E5%87%BA? %0A
     
@@ -1710,170 +1710,8 @@
 on%7B%0A
-        //String value = GetValueByKey(%22Test.properties%22, %22name%22);%0A        //System.out.println(value);%0A        //GetAllProperties(%22data/comtConfig.properties%22);%0A
     

Update PropertiesUtil.java