用JAVA轻松操作properties文件 (Read and Write Properties File)
不用介绍了, 所有的代码都写在这里了, 希望这个对你们有用。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
/**
*
* @author Fei
*/
public class PropertyUtils {
public PropertyUtils(){}
//1). Read value based on the value of key
public String readValue(String filePath,String key) {
Properties props = new Properties();
try{
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value= props.getProperty(key);
System.out.println("Class:PropertyUtils, Method:readValue, key:"+key+",value:"+value);
return value;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
//2). Reads an enumeration of keys from a file
public Enumeration getKeys(String filePath){
Enumeration en;
Properties props = new Properties();
try{
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
en = props.propertyNames();
/*
while(en.hasMoreElements()){
String key = (String)en.nextElement();
String property = props.getProperty(key);
//for testing purpose
System.out.println(key+property);
return en;
}
* */
return en;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
//3). get the all the keys and values as a Object Properties
public Properties readProperties(String filePath){
Properties props = new Properties();
try{
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
return props;
}catch(Exception ex){
Debug.log(this, "readProperties", "Error occured during loading property file");
return null;
}
}
//4). Write to specific property file
public void writeProperties(String filePath,String parameterKey,String parameterValue){
Debug.log(this, "wirteProperty", "File Path:"+filePath);
Properties prop = new Properties();
try{
InputStream fis = new FileInputStream(filePath);
//read key value pair from file input stream
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
/*
* Calls the Hashtable method put. Provided for parallelism with the getProperty method.
* Enforces use of strings for property keys and values.
* The value returned is the result of the Hashtable call to put.
*
* */
prop.setProperty(parameterKey, parameterValue);
/*
* Writes this property list (key and element pairs) in this Properties table to
* the output stream in a format suitable for loading into a Properties table
* using the load method
* */
prop.store(fos, "Last Update, key:"+parameterKey+",value: "+parameterValue);
}catch(Exception ex){
System.err.println("Visit "+filePath+" for updating "+parameterKey+" value error");
}
}
//5). Remove one record based on the key specified
public void removeProperty(String filePath,String key){
Debug.log(this, "removeProperty", "File Path:"+filePath);
Properties props = new Properties();
try{
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
props.remove(key);
//create a new file output stream to hold the property file in which
//entry has been deleted.
OutputStream fos = new FileOutputStream(filePath);
props.store(fos, "Last Update,"+new Date()+" Deletion: key:"+key);
}catch(Exception ex){
ex.printStackTrace();
}
}
//6). Update record based on key specified
public void updateProperty(String filePath,String key,String value){
Debug.log(this, "removeProperty", "File Path:"+filePath);
Properties props = new Properties();
try{
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
props.setProperty(key, value);
OutputStream fos = new FileOutputStream(filePath);
props.store(fos, "Last Update, key:"+key+",value: "+value);
}catch(Exception ex){
ex.printStackTrace();
}
}
public boolean checkKeyAvailability(String filePath, String key){
boolean flag = true;//By Default all keys all available
Enumeration keys = this.getKeys(filePath);
while(keys.hasMoreElements()){
String keyStr = (String)keys.nextElement();
if(keyStr.equalsIgnoreCase(key)){
flag=false;
}
}
return flag;
}
/*
public static void main(String[] args) {
//readValue("info.properties","url");
//writeProperties("info.properties","age","21");
readProperties("c://java//info.properties" );
System.out.println("OK");
}*/
}