scala Scala获取资源文件夹中文件的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23831768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:16:28  来源:igfitidea点击:

Scala get file path of file in resources folder

scalaiofilepathstanford-nlp

提问by user1893354

I am using the Stanford CRFClassifier and in order to run, it requires a file that is the trained classifier model. I have put this file in the resources directory. From the Javadocs for the CRFClassifier http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ie/crf/CRFClassifier.html#getClassifier(java.lang.String)the path to the file must be an input to CRFClassifier.getClassifier() and it is a java.lang.String object. So my question is how do I tell .getClassifier() that the file is in the resources directory? i.e. how do I get the file path of a file in the resources directory?

我正在使用斯坦福 CRFClassifier,为了运行,它需要一个经过训练的分类器模型的文件。我已将此文件放在资源目录中。从 CRFClassifier 的 Javadocs http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ie/crf/CRFClassifier.html#getClassifier(java.lang.String)文件的路径必须是 CRFClassifier.getClassifier() 的输入,它是一个 java.lang.String 对象。所以我的问题是如何告诉 .getClassifier() 该文件在资源目录中?即如何获取资源目录中文件的文件路径?

I have tried simply

我试过简单

val classifier = CRFClassifier.getClassifier("./src/main/resources/my_model.ser.gz")

But this returns a FileNotFoundException.

但这会返回 FileNotFoundException。

I have also tried

我也试过

Source.fromURL(getClass.getResource("/my_model.ser.gz"))

which returns a BufferedSource object, but I do not know how to get a file path from this.

它返回一个 BufferedSource 对象,但我不知道如何从中获取文件路径。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by user1893354

I managed to be able to get the file path by doing the following

我设法通过执行以下操作来获取文件路径

val url=getClass.getResource("/my_model.ser.gz")

val url=getClass.getResource("/my_model.ser.gz")

val classifier = CRFClassifier.getClassifier(url.getPath())

val classifier = CRFClassifier.getClassifier(url.getPath())