How can I load properties file located under src/main/resources/utility from classpath?
2 Answers
Properties prop = new Properties(); try { // load a properties file prop.load(new FileInputStream("src/main/resources/utility/config.properties")); // get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } Example retrieved from http://www.mkyong.com/java/java-properties-file-examples/
2 Comments
Gyro Gearless
It is usually a bad idea to reference the
src/ folder but at compile time, never at runtime. Rather use utility/config.properties, as this matches the runtime classpath.andPat
Infact #luanjot, you mislead the section, was section 3 with properties file from classpath, more correct way is
prop.load(App.class.getClassLoader().getResourceAsStream("config.properties")));, however thank you very much!getResourceAsStream will locate the files relative to the "root" of the classpath
initFile - File Location:
init file can also be packed as a part of war.
Properties props = new Properties(); InputStream uin = this.getClass().getResourceAsStream(initFile); InputStreamReader isr = new InputStreamReader(uin); BufferedReader in = new BufferedReader(new InputStreamReader(uin)); props.load(uin);