Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put a text file in Grails, and how to get the path

I need to read in a .txt file into a groovy class in order to interrogate it line by line. But I am not sure what folder I put it into in my grails app, and how to get the path to it?

So far I have tried placing it under src and also in a new folder web-app/txt

and I have tried the the following to read it in

fileIn = new File('/lexicon.txt').text 

and

fileIn = new File('txt/lexicon.txt').text

to no avail.

Any body have any pointers?

like image 516
andy mccullough Avatar asked Mar 13 '13 15:03

andy mccullough


People also ask

How do I find the path of a text file?

Click at the end of the box that contains the file name. It's just above the list of files inside the folder, and just below the icons. This highlights the full path to the file. To copy the path, press Ctrl + C .

Can we monitor a directory for adding new files in Java?

Create a WatchService "watcher" for the file system. For each directory that you want monitored, register it with the watcher. When registering a directory, you specify the type of events for which you want notification. You receive a WatchKey instance for each directory that you register.

How do you use Grails?

Go to start.grails.org and use the Grails Application Forge to generate your Grails project. You can choose your project type (Application or Plugin), pick a version of Grails, and choose a Profile - then click "Generate Project" to download a ZIP file. No Grails installation necessary!


4 Answers

Grails is a Java Web Application, so it will be compiled into a sigle file .war, with all files/classes/etc inside. Most Web containers do unpack war, but there are no any guaranteee, so it's not a good idea to use File to access this file as a file.

Btw, you can place your file into grails-app/conf, at this case it will be placed into classpath, and you'll be able to access it by using:

InputStream lexicon = this.class.classLoader.getResourceAsStream('lexicon.txt')

You could also put this file into a subdirectory, like grails-app/conf/data and load it as ***.getResourceAsStream('data/lexicon.txt')

like image 151
Igor Artamonov Avatar answered Oct 03 '22 11:10

Igor Artamonov


You can put your file under web-app/

Example:

web-app/lexicon.txt

And then in your controller or service use grailsApplication:

class MyService {
    def grailsApplication
    public myMethod() {
        File myFile = grailsApplication.mainContext.getResource("lexicon.txt").file
    }
}

Hope this helps

like image 20
Roberto Perez Alcolea Avatar answered Oct 03 '22 09:10

Roberto Perez Alcolea


You can use Spring's resource loading to access the file. With this method you can access the file from a Spring bean, which means Grails can autowire the resource in to its artifacts.

See below for the following steps examples

  1. Place the file in grails-app/conf/.
  2. Make a resource holder class in src/groovy
  3. Add the resource holder as a Spring bean in grails-app/spring/resources.groovy
  4. Then autowire and use the resource wherever you need it

Step 2:

package resource

import org.springframework.core.io.Resource

class ResourceHolder {
    Resource lexicon
}

Step 3:

beans = {
    lexiconHolder(resource.ResourceHolder) {
        lexicon = 'classpath:lexicon.txt'
    }
}

Step 4:

class AnyGrailsService {
    def lexiconHolder

    void aMethodUsingTheLexicon() {
        File lexicon = lexiconHolder.lexicon.file

        /* Do stuff with the lexicon */
    }
like image 26
schmolly159 Avatar answered Oct 03 '22 10:10

schmolly159


In Grails 2, you can use the Grails Resource Locator

class MyService {
    def grailsResourceLocator

    myMethod() {
        def fileIn = grailsResourceLocator.findResourceForURI('/txt/lexicon.txt').file
    }
}

Handy tip: to mock this in Spock, use GroovyPageStaticResourceLoader

@TestFor(MyService)
class MyServiceSpec extends Specification {
    def setup() {
        service.grailsResourceLocator = Mock(GroovyPageStaticResourceLocator)
    }
}
like image 40
Keith Avatar answered Oct 03 '22 10:10

Keith