logo

How to easily read data from properties file in Java

How to easily read data from .properties file in Java.

Properties files are in this format: key = value, where each key is on one line. You can easily read properties from some file using object of type Propeties, but first you must open a stream. Next create instance of class Properties and load properties from file. After that you can easily get properties using method getProperty(key) and the result will be value from properties file.

try (FileReader reader = new FileReader("db.properties")) {
	Properties properties = new Properties();
	properties.load(reader);
	String url = properties.getProperty("db.url");
	System.out.println(url);
} catch (IOException e) {
	e.printStackTrace();
}

comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search