Created
December 1, 2015 02:38
-
-
Save tkt028/71578b3547ff54db1b73 to your computer and use it in GitHub Desktop.
Load properties from files in Java
This file contains hidden or 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
// Java : GetResourceAsStream In Static Method | |
// - http://www.mkyong.com/java/java-getresourceasstream-in-static-method/ | |
// If the method getFilePathToSave() is converted to static method, the getClass() | |
// method will be failed, and prompts "Cannot make a static reference to the non-static | |
// method getClass() from the type Object". | |
// Instead, you should use CurrentClass.class.getClassLoader().getResourceAsStream. | |
package com.mkyong.crawler.util; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Properties; | |
public class FileHelper { | |
public static String getFilePathToSave() { | |
Properties prop = new Properties(); | |
String filePath = ""; | |
try { | |
InputStream inputStream = | |
FileHelper.class.getClassLoader().getResourceAsStream("config.properties"); | |
prop.load(inputStream); | |
filePath = prop.getProperty("json.filepath"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return filePath; | |
} | |
} |
This file contains hidden or 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
// Loading A Property File From The Classpath | |
// - http://www.dzone.com/snippets/loading-property-file | |
private Properties getPropertiesFromClasspath(String propFileName) throws IOException { | |
// loading xmlProfileGen.properties from the classpath | |
Properties props = new Properties(); | |
InputStream inputStream = this.getClass().getClassLoader() | |
.getResourceAsStream(propFileName); | |
if (inputStream == null) { | |
throw new FileNotFoundException("property file '" + propFileName | |
+ "' not found in the classpath"); | |
} | |
props.load(inputStream); | |
return props; | |
} |
This file contains hidden or 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
// Java Properties File Examples - mkyong | |
// - http://www.mkyong.com/java/java-properties-file-examples/ | |
// Four sample codes: | |
// 1. Write to properties file | |
// 2. Load a properties file | |
// 3. Load a properties file from classpath | |
// 4. Prints everything from a properties file | |
// ================================================================= | |
// Code example of Loading a properties file from classpath | |
// Load a properties file config.properties from project classpath, and retrieved | |
// the property value. | |
// | |
// P.S Assume properties file "config.properties" is in your project classpath | |
// root folder. | |
package com.mkyong.properties; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Properties; | |
public class App { | |
public static void main( String[] args ){ | |
Properties prop = new Properties(); | |
InputStream input = null; | |
try { | |
String filename = "config.properties"; | |
input = App3.class.getClassLoader().getResourceAsStream(filename); | |
if(input==null){ | |
System.out.println("Sorry, unable to find " + filename); | |
return; | |
} | |
//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("database")); | |
System.out.println(prop.getProperty("dbuser")); | |
System.out.println(prop.getProperty("dbpassword")); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} finally{ | |
if(input!=null){ | |
try { | |
input.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
// @Output: | |
// | |
// localhost | |
// mkyong | |
// password | |
// For non-static method, use this : | |
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties")); | |
// ================================================================= |
Hello,
Im getting a resource leak in my code from props.load.
any idea how I can fix this issue? I tried props.clear() and it did not get rid of the issue.
Any help will be greatly appreciated.
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice thanks