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

前端 未结 4 1067
感动是毒
感动是毒 2020-12-13 09:41

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

4条回答
  •  时光取名叫无心
    2020-12-13 10:27

    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')

提交回复
热议问题