Reading Local Data Files in Grails

By

I recently worked on a project in Grails 3.x that required migrating legacy data from a local .csv file during Bootstrap. I needed the data from this file to populate my Domains the very first time the application was run.

I was confused about two things:

  1. Where do I put the file?
  2. How do I retrieve the file from inside Bootstrap.groovy?

After searching the inter-webs, I finally found a solution that worked for me. I ended up creating a sub-directory in grails-app/conf called /data. All of the files in this data directory would automatically be placed into the classpath and could be accessed from inside Bootstrap.groovy like so:

InputStream myData = this.class.classLoader.getResourceAsStream('/data/<FILENAME>.csv');

… And that’s all there is to it! I’m sure there are many other solutions to this problem, but since my data file was a primarily a configuration artifact, it made sense to me to include it in the conf directory.