package ie.dcu.util; import java.io.*; import java.util.*; /** * Utility functions for .properties files. * * @author Kevin McGuinness */ public class PropsUtils { /** * Save the properties object to a file. * * @param props * The properties. * @param file * The file to save it to. * @throws IOException * If there is an error saving. */ public static void save(Properties props, File file) throws IOException { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); props.store(out, "Automatically generated properties file"); } finally { if (out != null) out.close(); } } /** * Load the properties object from a file. * * @param file * The file. * @return A new properties object. * @throws IOException * If an error occurs loading the properties. */ public static Properties load(File file) throws IOException { InputStream in = null; Properties props = null; try { in = new BufferedInputStream(new FileInputStream(file)); props = new Properties(); props.load(in); } finally { if (in != null) in.close(); } return props; } public static File getFile(Properties props, String key) { String val = props.getProperty(key); if (val != null) { return new File(val); } return null; } }