Created
June 1, 2011 19:42
-
-
Save ran488/1003124 to your computer and use it in GitHub Desktop.
Utility class for reading and writing Properties files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.XXXX.XXX.common.util; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Properties; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
@VersionId("@(#) PropertiesUtil.java [@@/main/ens_java_template/1]>") | |
public class PropertiesUtil extends EnsBaseObject { | |
private final static Log log = LogFactory.getLog(PropertiesUtil.class); | |
/** Singleton instance */ | |
private static PropertiesUtil _instance; | |
private PropertiesUtil() { | |
super(); | |
} | |
/** Singleton factory method - lazy load */ | |
public static synchronized PropertiesUtil instance() { | |
if (_instance == null) | |
_instance = new PropertiesUtil(); | |
return _instance; | |
} | |
/** | |
* Reads a properties (key=value) file into a java.util.Properties object. | |
* | |
* @param filename | |
* @return | |
* @throws FileNotFoundException | |
* @throws IOException | |
*/ | |
public Properties readPropertiesFromFile(String filename) | |
throws FileNotFoundException, IOException { | |
Properties p = null; | |
FileReader fr = null; | |
try { | |
p = new Properties(); | |
fr = new FileReader(filename); | |
p.load(fr); | |
} finally { | |
fr.close(); | |
} | |
return p; | |
} | |
/** | |
* Creates a properties file (key=value), filename, given a | |
* java.util.Properties object | |
* | |
* @param filename | |
* @param properties | |
* @throws IOException | |
*/ | |
public void writePropertiesToFile(String filename, Properties properties) | |
throws IOException { | |
FileWriter fw = null; | |
try { | |
fw = new FileWriter(filename); | |
properties.store(fw, "Properties file created on " | |
+ new java.util.Date().toString()); | |
} finally { | |
fw.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment