Yesterday we try to write the .properties file.
Java – Write .properties
But most of the time we would like to read them file instead. The following Class demonstrate how to write the config.properties.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertyFile {
public static void main(String[] args) {
Properties prop = new Properties();
try {
// Load the property file
prop.load(new FileInputStream("src/main/resources/config.properties"));
// Get and print the values
System.out.println(prop.getProperty("name"));
System.out.println(prop.getProperty("url"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
This is the src/main/resources/config.properties.
#Sat Aug 27 23:02:57 CST 2011 url=http\://ykyuen.wordpress.com name=Eureka
The console output.
Eureka
https://ykyuen.wordpress.com
Done =)
Reference: Mkyong.com – Java Properties file examples
