Skip to content

Instantly share code, notes, and snippets.

@andrea-ale-sbarra
Created May 17, 2016 13:20
Show Gist options
  • Save andrea-ale-sbarra/b3185be8c7b8c82dec55a25d79918895 to your computer and use it in GitHub Desktop.
Save andrea-ale-sbarra/b3185be8c7b8c82dec55a25d79918895 to your computer and use it in GitHub Desktop.
read .properties from classpath
Properties prop = new Properties();
InputStream input = null;
try {
String filename = "config.properties";
input = this.getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
throw new java.lang.UnsupportedOperationException("Some errors occurred, please try again later.");
}
// load a properties file from class path, inside static method
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("filsystem.root"));
} catch (IOException ex) {
throw new java.lang.UnsupportedOperationException("Some errors occurred, please try again later.");
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
throw new java.lang.UnsupportedOperationException("Some errors occurred, please try again later.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment