Using properties files in Grails application
Posted By : Akash Sharma | 25-Aug-2013
In this blog I am going to share the details of how to read and write into a properties file in grails.
You can create a file(except .properties file) manually/programmatically in application context path and read or write data into it.But using a properties file is a standard way and its very easy to read and write data from it.
Before going to inner details of how to read and write data from a properties file I want demonstrate the path where it is created.
First of all you can create properties with any name you want.But make it specific with your project. e.g: myProjectNameProperties.properties
While you are working in an STS , properties file will be created in project location on your system.You can get this path as follows:
right click on project in STS
properties-->Resources
Location field will show the project location.
While you are working in a web/application server for example tomcat , you will find the properties file in tomcat_home/bin/ folder.
Now lets start with creating a properties file in our grails project.
You have to configure your config.groovy file by adding a line in it as follows:
grails.config.locations =["classpath:myPropertiesFile.properties"]
Now you have to load properties file for the first time.For this you can write loading part of code at BootStrap.groovy.
class BootStrap { def init = { servletContext -> setPropertiesData() } def destroy = { } void setPropertiesData() { String key="city" String value="delhi" String propertyFileName="myPropertiesFile.properties" File file = new File(propertyFileName) Properties prop = new Properties(); try { prop.setProperty(key, value); prop.store(new FileOutputStream(file), null); log.debug"key:"+key+" has value:"+value+" in "+propertyFileName } catch (IOException e) { log.debug"exception occured while saving properties file :"+e } } }
Now after execution of this code you verify properties file location.
Next thing is to read data from it.For this I have an action in my controller which reads data from it.
def getPropertiesData={ String key="city" String propertyFileName="myPropertiesFile.properties" Properties prop = new Properties(); try { FileInputStream inputStream = new FileInputStream(propertyFileName) prop.load(inputStream); String cityName = prop.getProperty(key); log.debug"key:"+key+" has value:"+cityName } catch (IOException e) { log.debug"Exception occured while reading properties file :"+e } }
Akash Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis