java¶ÁÈ¡propertiesÎļþ
Ò»£® .properties ÎļþµÄÐÎʽ ==========================================================
# ÒÔÏÂΪ·þÎñÆ÷¡¢Êý¾Ý¿âÐÅÏ¢
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# ÒÔÏÂΪÊý¾Ý¿â±íÐÅÏ¢
dbTable = mytable
# ÒÔÏÂΪ·þÎñÆ÷ÐÅÏ¢
ip = 192.168.0.9
¡¤¡¤¡¤¡¤¡¤¡¤
ÔÚÉÏÃæµÄÎļþÖÐÎÒÃǼÙÉè¸ÃÎļþÃûΪ£º test.properties Îļþ¡£ÆäÖÐ # ¿ªÊ¼µÄÒ»ÐÐΪעÊÍÐÅÏ¢£»ÔڵȺš° = ¡±×ó±ßµÄÎÒÃdzÆ֮Ϊ key £»µÈºÅ¡° = ¡±ÓұߵÄÎÒÃdzÆ֮Ϊ value ¡££¨Æäʵ¾ÍÊÇÎÒÃdz£ËµµÄ¼ü ¨C Öµ¶Ô£© key Ó¦¸ÃÊÇÎÒÃdzÌÐòÖеıäÁ¿¡£¶ø value ÊÇÎÒÃǸù¾Ýʵ¼ÊÇé¿öÅäÖõġ£
¶þ£® JDK ÖÐµÄ Properties Àà Properties Àà´æÔÚÓÚ°û Java.util ÖУ¬¸ÃÀà¼Ì³Ð×Ô Hashtable £¬ËüÌṩÁ˼¸¸öÖ÷ÒªµÄ·½·¨£º 1£® getProperty ( String key) £¬ ÓÃÖ¸¶¨µÄ¼üÔÚ´ËÊôÐÔÁбíÖÐËÑË÷ÊôÐÔ¡£Ò²¾ÍÊÇͨ¹ý²ÎÊý key £¬µÃµ½ key Ëù¶ÔÓ¦µÄ value ¡£
2£® load ( InputStream inStream) £¬´ÓÊäÈëÁ÷ÖжÁÈ¡ÊôÐÔÁÐ±í£¨¼üºÍÔªËضԣ©¡£Í¨¹ý¶ÔÖ¸¶¨µÄÎļþ£¨±ÈÈç˵ÉÏÃæµÄ test.properties Îļþ£©½øÐÐ×°ÔØÀ´»ñÈ¡¸ÃÎļþÖеÄËùÓмü ¨C Öµ¶Ô¡£ÒÔ¹© getProperty ( String key) À´ËÑË÷¡£ 3£® setProperty ( String key, String value) £¬µ÷Óà Hashtable µÄ·½·¨ put ¡£Ëûͨ¹ýµ÷ÓûùÀàµÄput·½·¨À´ÉèÖà ¼ü ¨C Öµ¶Ô¡£
4£® store ( OutputStream out, String comments) £¬ ÒÔÊʺÏʹÓà load ·½·¨¼ÓÔص½ Properties ±íÖеĸñʽ£¬½«´Ë Properties ±íÖеÄÊôÐÔÁÐ±í£¨¼üºÍÔªËضԣ©Ð´ÈëÊä³öÁ÷¡£Óë load ·½·¨Ïà·´£¬¸Ã·½·¨½«¼ü ¨C Öµ¶ÔдÈëµ½Ö¸¶¨µÄÎļþÖÐÈ¥¡£
5£® clear () £¬Çå³ýËùÓÐ×°ÔØµÄ ¼ü ¨C Öµ¶Ô¡£¸Ã·½·¨ÔÚ»ùÀàÖÐÌṩ¡£
ÓÐÁËÒÔÉϼ¸¸ö·½·¨ÎÒÃǾͿÉÒÔ¶Ô .properties Îļþ½øÐвÙ×÷ÁË£¡
Èý£®´úÂëʵÀý
package configuration;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/** *//**
* ¶ÁÈ¡propertiesÎļþ
* @author Qutr
*
*/
public class Configuration
¡{
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;
/** *//**
* ³õʼ»¯ConfigurationÀà
*/
public Configuration()
¡{
propertie = new Properties();
}
/** *//**
* ³õʼ»¯ConfigurationÀà
* @param filePath Òª¶ÁÈ¡µÄÅäÖÃÎļþµÄ·¾¶+Ãû³Æ
*/
public Configuration(String filePath)
¡{
propertie = new Properties();
try ¡{
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) ¡{
System.out.println(¡°¶ÁÈ¡ÊôÐÔÎļþ¡ª>ʧ°Ü£¡- ÔÒò£ºÎļþ·¾¶´íÎó»òÕßÎļþ²»´æÔÚ¡±);
ex.printStackTrace();
} catch (IOException ex) ¡{
System.out.println(¡°×°ÔØÎļþ¡ª>ʧ°Ü!¡±);
ex.printStackTrace();
}
}//end ReadConfigInfo(¡)
/** *//**
* ÖØÔغ¯Êý£¬µÃµ½keyµÄÖµ
* @param key È¡µÃÆäÖµµÄ¼ü
* @return keyµÄÖµ
*/
public String getValue(String key)
¡{
if(propertie.containsKey(key))¡{
String value = propertie.getProperty(key);//µÃµ½Ä³Ò»ÊôÐÔµÄÖµ
return value;
}
else
return ¡°¡±;
}//end getValue(¡)
/** *//**
* ÖØÔغ¯Êý£¬µÃµ½keyµÄÖµ
* @param fileName propertiesÎļþµÄ·¾¶+ÎļþÃû
* @param key È¡µÃÆäÖµµÄ¼ü
* @return keyµÄÖµ
*/
public String getValue(String fileName, String key)
¡{
try ¡{
String value = ¡°¡±;
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();
if(propertie.containsKey(key))¡{
value = propertie.getProperty(key);
return value;
}else
return value;
} catch (FileNotFoundException e) ¡{
e.printStackTrace();
return ¡°¡±;
} catch (IOException e) ¡{
e.printStackTrace();
return ¡°¡±;
} catch (Exception ex) ¡{
ex.printStackTrace();
return ¡°¡±;
}
}//end getValue(¡)
/** *//**
* Çå³ýpropertiesÎļþÖÐËùÓеÄkeyºÍÆäÖµ
*/
public void clear()
¡{
propertie.clear();
}//end clear();
/** *//**
* ¸Ä±ä»òÌí¼ÓÒ»¸ökeyµÄÖµ£¬µ±key´æÔÚÓÚpropertiesÎļþÖÐʱ¸ÃkeyµÄÖµ±»valueËù´úÌ棬
* µ±key²»´æÔÚʱ£¬¸ÃkeyµÄÖµÊÇvalue
* @param key Òª´æÈëµÄ¼ü
* @param value Òª´æÈëµÄÖµ
*/
public void setValue(String key, String value)
¡{
propertie.setProperty(key, value);
}//end setValue(¡)
/** *//**
* ½«¸ü¸ÄºóµÄÎļþÊý¾Ý´æÈëÖ¸¶¨µÄÎļþÖУ¬¸ÃÎļþ¿ÉÒÔÊÂÏȲ»´æÔÚ¡£
* @param fileName Îļþ·¾¶+ÎļþÃû³Æ
* @param description ¶Ô¸ÃÎļþµÄÃèÊö
*/
public void saveFile(String fileName, String description)
¡{
try ¡{
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotF