scala SBT 项目中的“资源”文件夹有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3868708/
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
What are "resources" folders in SBT projects for?
提问by Ivan
In SBT project folders hierarchy I am to put my Scala sources in src/main/scala and tests in src/tests/scala. What am I meant to put into src/main/resources and src/tests/resources?
在 SBT 项目文件夹层次结构中,我将我的 Scala 源代码放在 src/main/scala 中,并将测试放在 src/tests/scala 中。我想在 src/main/resources 和 src/tests/resources 中放入什么?
采纳答案by Dylan Lacey
Everything in that directory gets packed into the .jar created when you call package.
该目录中的所有内容都会打包到您调用 .jar 时创建的 .jar 中package。
This means you can use it for images, sound files, text, anything that's not code but is used by your code.
这意味着您可以将它用于图像、声音文件、文本以及任何不是代码但被您的代码使用的东西。
回答by Eugene Yokota
Here's an example of copying a text file stored in resource to a local file system:
这是将存储在资源中的文本文件复制到本地文件系统的示例:
def copyFileFromResource(source: String, dest: File) {
val in = getClass.getResourceAsStream(source)
val reader = new java.io.BufferedReader(new java.io.InputStreamReader(in))
val out = new java.io.PrintWriter(new java.io.FileWriter(dest))
var line: Option[String] = None
line = Option[String](reader.readLine)
while (line != None) {
line foreach { out.println }
line = Option[String](reader.readLine)
}
in.close
out.flush
}

