Load Java Properties file from relative or absolute path

/**
 * Load properties.
 *
 * @param propertiesFile
 *            path to properties file
 * @return filled in properties object
 * @throws IOException
 *             if something goes wrong
 */
private Properties loadProperties(final String propertiesFile)
      throws IOException {
   // Properties object we are going to fill up.
   Properties properties = new Properties();
   // If file exists as an absolute path, load as input stream.
   final Path path = Paths.get(propertiesFile);
   if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
      properties.load(new FileInputStream(propertiesFile));
   } else {
      // Otherwise, use resource as stream.
      properties.load(getClass().getClassLoader().getResourceAsStream(
            propertiesFile));
   }
   return properties;
}

Popular Posts