【问题标题】:Where to put a text file in Grails, and how to get the path在 Grails 中放置文本文件的位置,以及如何获取路径
【发布时间】:2013-03-01 14:59:46
【问题描述】:

我需要将 .txt 文件读入一个 groovy 类,以便逐行查询它。但我不确定我将它放在我的 grails 应用程序中的哪个文件夹,以及如何获取它的路径?

到目前为止,我已尝试将其放在 src 下以及 web-app/txt 的新文件夹中

我已经尝试了以下方法来阅读它

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

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

无济于事。

任何正文都有指针?

【问题讨论】:

    标签: java file grails inputstream


    【解决方案1】:

    Grails 是一个 Java Web 应用程序,因此它将被编译成一个单独的文件.war,其中包含所有文件/类/等。大多数 Web 容器都会解包 war,但没有任何保证,因此使用 File 将这个文件作为文件访问不是一个好主意。

    顺便说一句,您可以将文件放入grails-app/conf,在这种情况下,它将被放入classpath,您可以通过以下方式访问它:

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

    你也可以把这个文件放到一个子目录中,比如grails-app/conf/data,然后加载为***.getResourceAsStream('data/lexicon.txt')

    【讨论】:

    • IMO 放杂项。 conf 目录下的文件,这不是一个完美的主意。
    • 它不仅仅是一个“杂项文件”,它很可能是一个配置工件
    • 如果文件被放到 src/main/resources 中,那么它们的访问方式与上面完全相同。 String fileNameAndPath = this.class.classLoader.getResource("actualFileName.txt").getFile()
    • 它适用于 grails v3.3.6,要将其用作 json,您需要添加:myFile.getText()然后解析它:JSON.parse(myFileAsText)
    【解决方案2】:

    你可以把你的文件放在 web-app/ 下

    例子:

    web-app/lexicon.txt
    

    然后在你的控制器或服务中使用 grailsApplication:

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您可以使用 Spring 的资源加载来访问该文件。使用此方法,您可以从 Spring bean 访问文件,这意味着 Grails 可以将资源自动装配到其工件中。

      以下步骤示例见下文

      1. 将文件放入grails-app/conf/
      2. src/groovy 中创建一个资源持有者类
      3. grails-app/spring/resources.groovy中添加资源持有者作为Spring bean
      4. 然后自动装配并在任何需要的地方使用资源

      第 2 步:

      package resource
      
      import org.springframework.core.io.Resource
      
      class ResourceHolder {
          Resource lexicon
      }
      

      第 3 步:

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

      第 4 步:

      class AnyGrailsService {
          def lexiconHolder
      
          void aMethodUsingTheLexicon() {
              File lexicon = lexiconHolder.lexicon.file
      
              /* Do stuff with the lexicon */
          }
      

      【讨论】:

        【解决方案4】:

        在 Grails 2 中,您可以使用 Grails 资源定位器

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

        小贴士:要在 Spock 中模拟它,请使用 GroovyPageStaticResourceLoader

        @TestFor(MyService)
        class MyServiceSpec extends Specification {
            def setup() {
                service.grailsResourceLocator = Mock(GroovyPageStaticResourceLocator)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-05
          • 2014-09-26
          • 2014-02-06
          相关资源
          最近更新 更多