在Grails中放置文本文件的位置,以及如何获取路径 [英] Where to put a text file in Grails, and how to get the path
问题描述
我需要将.txt文件读入groovy类,以便逐行进行查询。但我不确定我的grails应用程序中放入了哪个文件夹,以及如何获取它的路径?
到目前为止,我已经尝试将它放在 src
,并在新文件夹 web-app / txt
我已经尝试了下面的内容来阅读它:
fileIn = new File('/ lexicon.txt')。text
和
<$文本
到no a bil。
任何机构都有任何指针? 解析方案
一个Java Web应用程序,所以它将被编译成一个sigle文件 .war
,其中包含所有文件/类/ etc。大多数Web容器都会解压缩 war
,但没有任何保证,因此使用 File
来并不是一个好主意作为一个文件访问这个文件。
顺便说一句,你可以将你的文件放在 grails-app / conf
中,在这种情况下,它将被放入 classpath
,您可以通过以下方式访问它:
InputStream lexicon = this.class.classLoader.getResourceAsStream('lexicon.txt')
您也可以把这个文件放到一个子目录中,比如 grails-app / conf / data
并加载它作为 ***。getResourceAsStream('data / lexicon.txt ')
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?
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')
这篇关于在Grails中放置文本文件的位置,以及如何获取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!