Read,Write and Update the Properities file using Java
Post date: Mar 23, 2011 9:40:55 AM
Write into properities file
public static void WriteToProperity(String key, String value,
String path) {
Properties prop = new Properties();
// FileInputStream inStream = new FileInputStream(path);
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(path, true);
// Update the environment varaible
prop.setProperty(key,value
prop.store(outStream, "Path Properity Files");
outStream.close();
} catch (Exception e) {
// TODO: handle exception
try {
outStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
//Delete key from properties files
public static void deleteKeyVariable(String key,String value,
String path) {
FileOutputStream outStream = null;
try {
Properties prop = new Properties();
// FileInputStream inStream = new FileInputStream(path);
FileInputStream inStream = new FileInputStream(path);
prop.load(inStream);
// Remove the key
prop.remove(key);
outStream = new FileOutputStream(path);
// prop.setProperty(environClass.getKey(), environClass.getValue());
prop.store(outStream, "Delete the Properity Files");
outStream.close();
} catch (Exception e) {
// TODO: handle exception
try {
outStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
//Read the values from properties files
public static List<EnvironmentClass> getEnviromentVaribale(
String properityPath) throws InstallerException {
List<EnvironmentClass> environmentList = new ArrayList<EnvironmentClass>();
Properties prop = new Properties();
EnvironmentClass environment;
String propKey = null;
String propValue = null;
try {
FileInputStream inStream = new FileInputStream(properityPath);
// load the form
prop.load(inStream);
Enumeration<Object> keys = prop.keys();
while (keys.hasMoreElements()) {
// Get the key
propKey = (String) keys.nextElement();
// Get the key values
propValue = prop.getProperty(propKey);
// Put into the List;
System.out
.println("Key =" + propKey + " Values: " + propValue);
environment = new EnvironmentClass(propKey, propValue);
environmentList.add(environment);
}
return environmentList;
} catch (IOException e) {
// TODO Auto-generated catch block
throw new InstallerException(e);
}
}